Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react: Convert class components to function components, where trivial #5509

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions src/boot/TranslationProvider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow strict-local */
import React, { PureComponent, type Context } from 'react';
import React, { type Context, useContext } from 'react';
import type { ComponentType, ElementConfig, Node } from 'react';
import { Text } from 'react-native';
import { IntlProvider, IntlContext } from 'react-intl';
Expand All @@ -23,14 +23,13 @@ export const TranslationContext: Context<GetText> = React.createContext(undefine
export function withGetText<P: { +_: GetText, ... }, C: ComponentType<P>>(
WrappedComponent: C,
): ComponentType<$ReadOnly<$Exact<$Diff<ElementConfig<C>, {| _: GetText |}>>>> {
return class extends React.Component<$Exact<$Diff<ElementConfig<C>, {| _: GetText |}>>> {
render() {
return (
<TranslationContext.Consumer>
{_ => <WrappedComponent _={_} {...this.props} />}
</TranslationContext.Consumer>
);
}
// eslint-disable-next-line func-names
return function (props: $Exact<$Diff<ElementConfig<C>, {| _: GetText |}>>): Node {
return (
<TranslationContext.Consumer>
{_ => <WrappedComponent _={_} {...props} />}
</TranslationContext.Consumer>
);
};
}

Expand Down Expand Up @@ -61,19 +60,14 @@ const makeGetText = (intl: IntlShape): GetText => {
*
* See the `GetTypes` type for why we like the new shape.
*/
class TranslationContextTranslator extends PureComponent<{|
+children: Node,
|}> {
static contextType = IntlContext;
context: IntlShape;
function TranslationContextTranslator(props: {| +children: Node |}): Node {
const intlContextValue = useContext(IntlContext);

render() {
return (
<TranslationContext.Provider value={makeGetText(this.context)}>
{this.props.children}
</TranslationContext.Provider>
);
}
return (
<TranslationContext.Provider value={makeGetText(intlContextValue)}>
{props.children}
</TranslationContext.Provider>
);
}

type Props = $ReadOnly<{|
Expand Down
28 changes: 13 additions & 15 deletions src/chat/FetchError.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow strict-local */

import React, { PureComponent } from 'react';
import React from 'react';
import type { Node } from 'react';
import { StyleSheet, View } from 'react-native';

Expand All @@ -26,18 +26,16 @@ type Props = $ReadOnly<{|
error: mixed,
|}>;

export default class FetchError extends PureComponent<Props> {
render(): Node {
return (
<View style={styles.container}>
{(() => {
if (this.props.error instanceof TimeoutError) {
return <ZulipTextIntl style={styles.text} text="Request timed out." />;
} else {
return <ZulipTextIntl style={styles.text} text="Oops! Something went wrong." />;
}
})()}
</View>
);
}
export default function FetchError(props: Props): Node {
return (
<View style={styles.container}>
{(() => {
if (props.error instanceof TimeoutError) {
return <ZulipTextIntl style={styles.text} text="Request timed out." />;
} else {
return <ZulipTextIntl style={styles.text} text="Oops! Something went wrong." />;
}
})()}
</View>
);
}
16 changes: 7 additions & 9 deletions src/chat/InvalidNarrow.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow strict-local */

import React, { PureComponent } from 'react';
import React from 'react';
import type { Node } from 'react';
import { StyleSheet, View } from 'react-native';

Expand All @@ -24,12 +24,10 @@ type Props = $ReadOnly<{|
narrow: Narrow,
|}>;

export default class InvalidNarrow extends PureComponent<Props> {
render(): Node {
return (
<View style={styles.container}>
<ZulipTextIntl style={styles.text} text="That conversation doesn't seem to exist." />
</View>
);
}
export default function InvalidNarrow(props: Props): Node {
return (
<View style={styles.container}>
<ZulipTextIntl style={styles.text} text="That conversation doesn't seem to exist." />
</View>
);
}
42 changes: 20 additions & 22 deletions src/common/CountOverlay.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow strict-local */

import React, { PureComponent } from 'react';
import React from 'react';
import type { Node } from 'react';
import { View } from 'react-native';

Expand All @@ -23,26 +23,24 @@ type Props = $ReadOnly<{|
unreadCount: number,
|}>;

export default class CountOverlay extends PureComponent<Props> {
render(): Node {
const { children, unreadCount } = this.props;
export default function CountOverlay(props: Props): Node {
const { children, unreadCount } = props;

return (
<View>
<ComponentWithOverlay
style={styles.button}
// It looks like what we want to match is 25 * 0.75, which is 18.75,
// https://github.com/react-navigation/react-navigation/blob/%40react-navigation/bottom-tabs%405.11.15/packages/bottom-tabs/src/views/TabBarIcon.tsx#L69
overlaySize={18.75}
overlayColor={BRAND_COLOR}
overlayPosition="top-right"
showOverlay={unreadCount > 0}
customOverlayStyle={styles.overlayStyle}
overlay={<UnreadCount count={unreadCount} />}
>
{children}
</ComponentWithOverlay>
</View>
);
}
return (
<View>
<ComponentWithOverlay
style={styles.button}
// It looks like what we want to match is 25 * 0.75, which is 18.75,
// https://github.com/react-navigation/react-navigation/blob/%40react-navigation/bottom-tabs%405.11.15/packages/bottom-tabs/src/views/TabBarIcon.tsx#L69
overlaySize={18.75}
overlayColor={BRAND_COLOR}
overlayPosition="top-right"
showOverlay={unreadCount > 0}
customOverlayStyle={styles.overlayStyle}
overlay={<UnreadCount count={unreadCount} />}
>
{children}
</ComponentWithOverlay>
</View>
);
}
24 changes: 11 additions & 13 deletions src/common/ErrorMsg.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow strict-local */
import React, { PureComponent } from 'react';
import React from 'react';
import type { Node } from 'react';
import { View } from 'react-native';

Expand Down Expand Up @@ -29,18 +29,16 @@ type Props = $ReadOnly<{|
*
* @prop error - The error message string.
*/
export default class ErrorMsg extends PureComponent<Props> {
render(): Node {
const { error } = this.props;
export default function ErrorMsg(props: Props): Node {
const { error } = props;

if (!error) {
return null;
}

return (
<View style={styles.field}>
<ZulipTextIntl style={styles.error} text={error} />
</View>
);
if (!error) {
return null;
}

return (
<View style={styles.field}>
<ZulipTextIntl style={styles.error} text={error} />
</View>
);
}
42 changes: 20 additions & 22 deletions src/common/GroupAvatar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow strict-local */
import React, { PureComponent } from 'react';
import React from 'react';
import type { Node } from 'react';
import { View } from 'react-native';
// $FlowFixMe[untyped-import]
Expand Down Expand Up @@ -34,28 +34,26 @@ type Props = $ReadOnly<{|
* @prop children - If provided, will render inside the component body.
* @prop onPress - Event fired on pressing the component.
*/
export default class GroupAvatar extends PureComponent<Props> {
render(): Node {
const { children, names, size, onPress } = this.props;
export default function GroupAvatar(props: Props): Node {
const { children, names, size, onPress } = props;

const frameSize = {
height: size,
width: size,
borderRadius: size / 8,
backgroundColor: Color(colorHashFromString(names.join(', ')))
.lighten(0.6)
.hex(),
};
const frameSize = {
height: size,
width: size,
borderRadius: size / 8,
backgroundColor: Color(colorHashFromString(names.join(', ')))
.lighten(0.6)
.hex(),
};

const iconColor = Color(colorHashFromString(names.join(', '))).string();
const iconColor = Color(colorHashFromString(names.join(', '))).string();

return (
<Touchable onPress={onPress}>
<View style={[styles.frame, frameSize]}>
<IconGroup size={size * 0.75} color={iconColor} />
{children}
</View>
</Touchable>
);
}
return (
<Touchable onPress={onPress}>
<View style={[styles.frame, frameSize]}>
<IconGroup size={size * 0.75} color={iconColor} />
{children}
</View>
</Touchable>
);
}
36 changes: 17 additions & 19 deletions src/common/KeyboardAvoider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow strict-local */
import React, { PureComponent } from 'react';
import React from 'react';
import type { Node } from 'react';
import { Platform, View } from 'react-native';
import type { ViewStyleProp } from 'react-native/Libraries/StyleSheet/StyleSheet';
Expand Down Expand Up @@ -39,24 +39,22 @@ type Props = $ReadOnly<{|
* `KeyboardAvoidingView`'s special props get passed straight through
* to that component.
*/
export default class KeyboardAvoider extends PureComponent<Props> {
render(): Node {
const { behavior, children, style, contentContainerStyle, keyboardVerticalOffset } = this.props;
export default function KeyboardAvoider(props: Props): Node {
const { behavior, children, style, contentContainerStyle, keyboardVerticalOffset } = props;

if (Platform.OS === 'android') {
return <View style={style}>{children}</View>;
}

return (
<KeyboardAvoidingView
behavior={behavior}
contentContainerStyle={contentContainerStyle}
// See comment on this prop in the jsdoc.
keyboardVerticalOffset={keyboardVerticalOffset}
style={style}
>
{children}
</KeyboardAvoidingView>
);
if (Platform.OS === 'android') {
return <View style={style}>{children}</View>;
}

return (
<KeyboardAvoidingView
behavior={behavior}
contentContainerStyle={contentContainerStyle}
// See comment on this prop in the jsdoc.
keyboardVerticalOffset={keyboardVerticalOffset}
style={style}
>
{children}
</KeyboardAvoidingView>
);
}
18 changes: 8 additions & 10 deletions src/common/SearchEmptyState.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow strict-local */
import React, { PureComponent } from 'react';
import React from 'react';
import type { Node } from 'react';
import { View } from 'react-native';

Expand All @@ -23,14 +23,12 @@ type Props = $ReadOnly<{|
text: string,
|}>;

export default class SearchEmptyState extends PureComponent<Props> {
render(): Node {
const { text } = this.props;
export default function SearchEmptyState(props: Props): Node {
const { text } = props;

return (
<View style={styles.container}>
<ZulipTextIntl style={styles.text} text={text} />
</View>
);
}
return (
<View style={styles.container}>
<ZulipTextIntl style={styles.text} text={text} />
</View>
);
}
24 changes: 10 additions & 14 deletions src/common/SectionHeader.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* @flow strict-local */
import React, { PureComponent } from 'react';
import type { Node, Context } from 'react';
import React, { useContext } from 'react';
import type { Node } from 'react';
import { View } from 'react-native';

import type { ThemeData } from '../styles';
import { ThemeContext, createStyleSheet } from '../styles';
import ZulipTextIntl from './ZulipTextIntl';

Expand All @@ -18,16 +17,13 @@ type Props = $ReadOnly<{|
text: string,
|}>;

export default class SectionHeader extends PureComponent<Props> {
static contextType: Context<ThemeData> = ThemeContext;
context: ThemeData;
export default function SectionHeader(props: Props): Node {
const { text } = props;
const themeData = useContext(ThemeContext);

render(): Node {
const { text } = this.props;
return (
<View style={[styles.header, { backgroundColor: this.context.backgroundColor }]}>
<ZulipTextIntl text={text} />
</View>
);
}
return (
<View style={[styles.header, { backgroundColor: themeData.backgroundColor }]}>
<ZulipTextIntl text={text} />
</View>
);
}
8 changes: 3 additions & 5 deletions src/common/SectionSeparator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow strict-local */
import React, { PureComponent } from 'react';
import React from 'react';
import type { Node } from 'react';
import { View } from 'react-native';

Expand All @@ -13,8 +13,6 @@ const styles = createStyleSheet({
},
});

export default class SectionSeparator extends PureComponent<{||}> {
render(): Node {
return <View style={styles.separator} />;
}
export default function SectionSeparator(props: {||}): Node {
return <View style={styles.separator} />;
}
Loading