|
| 1 | +# [Standard] Writing a Component |
| 2 | + |
| 3 | +## Owner: Nicolas Djambazian |
| 4 | + |
| 5 | +## Standards |
| 6 | + |
| 7 | +**A react component begins than a `// @flow`** |
| 8 | + |
| 9 | +Why ? Flow typings will be used to check the Props and the State |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +**A react component should import React with the following syntax** |
| 14 | +``` |
| 15 | +import * as React from 'react'; |
| 16 | +``` |
| 17 | +Why ? To be able o access to the flow defined properties : https://flow.org/en/docs/react/types/ |
| 18 | + |
| 19 | + |
| 20 | +**The props and the state must be checked by flow** |
| 21 | + |
| 22 | +Why ? It allows to : |
| 23 | + |
| 24 | + - Check the props are valid when you use the component |
| 25 | + - Be sure to have done all checks for nullable props |
| 26 | + - Give a good updated documentation of the props |
| 27 | + |
| 28 | + |
| 29 | +Ex : |
| 30 | +``` |
| 31 | +type Props = { |
| 32 | + title?: ?string |
| 33 | + id: number, |
| 34 | + cards: Array<{ |
| 35 | + title: string, |
| 36 | + }>, |
| 37 | +} |
| 38 | +type State = { isOpenend: boolean }; |
| 39 | +class MyComponent extends React.PureComponent<Props, State> { |
| 40 | + // ... |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +**When we open a component file, we should see elements in that orders : props and state typings, class definition, style, hoc and then the final export** |
| 45 | + |
| 46 | +Why ? The first thing you want to know when you open a component is his API. So the typing should be as first. |
| 47 | + |
| 48 | +Then You want to know how it works and/or what it contains. So the class should be in seconds. |
| 49 | + |
| 50 | +Styling is the last thing you want to know. |
| 51 | + |
| 52 | + |
| 53 | +**The styling of a component should be in the same file at the end** |
| 54 | + |
| 55 | +The style of a component is highlty coupled with his implementation. When you want to change the DOM, you often have to change the style and vice-versa. |
| 56 | + |
| 57 | +If you want to use the same DOM with different styles, add a `style` props on your component. |
| 58 | + |
| 59 | +If another component need a part of your style, you have several choices : |
| 60 | + - Create a third component with that style, used by both of them |
| 61 | + - Add this part of the style in a theme |
| 62 | + |
| 63 | + |
| 64 | +**The Component should be exported as `default`** |
| 65 | + |
| 66 | +To not have to think of the name of the export. |
| 67 | + |
| 68 | +**The `render` function should come last** |
| 69 | + |
| 70 | +To immediately know where to look for the `render` function. |
| 71 | + |
| 72 | +**Instance methods should not be prefixed with `_`** |
| 73 | + |
| 74 | +It is common practice to add `_` in front of the private methods of your components. |
| 75 | +It turns out that *most* component methods are private. It would mean that most of our methods would need a `_`. |
| 76 | + |
| 77 | +To not forget any `_`, we simply choose to not put any. |
| 78 | + |
| 79 | +## Bad Example |
| 80 | + |
| 81 | + |
| 82 | +```jsx |
| 83 | +// No @flow |
| 84 | +import React from 'react'; // Bad React import |
| 85 | +import { Text, View } from 'react-native'; |
| 86 | + |
| 87 | +// style in an other file. |
| 88 | +import centeredStyle from '../../../style'; |
| 89 | + |
| 90 | +// Style at the begining of the file |
| 91 | +const styles = { |
| 92 | + centeredStyle, |
| 93 | + text: { |
| 94 | + color: '#bbbbbb' |
| 95 | + }, |
| 96 | +}; |
| 97 | + |
| 98 | +// No flow Props typing and no default export |
| 99 | +export class Page extends React.PureComponent { |
| 100 | + render() { |
| 101 | + return ( |
| 102 | + <View style={centeredStyle}> |
| 103 | + <Text style={styles.text}>{this.props.text}</Text> |
| 104 | + </View> |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | +## Good Example |
| 113 | +```jsx |
| 114 | +// @flow |
| 115 | +import * as React from 'react'; |
| 116 | +import { Text } from 'react-native'; |
| 117 | +import { CenteredPageContent } from '../components'; |
| 118 | + |
| 119 | +type Props = { |
| 120 | + text: string, |
| 121 | +} |
| 122 | + |
| 123 | +class Page extends React.PureComponent<Props> { |
| 124 | + render() { |
| 125 | + return ( |
| 126 | + <CenteredPageContent> |
| 127 | + <Text style={styles.text}>{this.props.text}</Text> |
| 128 | + </CenteredPageContent> |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +const styles = { |
| 134 | + text: { |
| 135 | + color: '#bbbbbb' |
| 136 | + }, |
| 137 | +}; |
| 138 | + |
| 139 | +export default Page; |
| 140 | +``` |
| 141 | + |
0 commit comments