|
| 1 | +# React |
| 2 | + |
| 3 | +# Naming |
| 4 | + |
| 5 | +* Always use JSX syntax. |
| 6 | + |
| 7 | +* Use .jsx extension for React components. |
| 8 | + |
| 9 | +* Use PascalCase for filenames. E.g., FoodMenus.jsx. |
| 10 | + |
| 11 | +# Syntax |
| 12 | + |
| 13 | +* Do not use React.createElement unless you're initializing the app from a file that is not JSX. |
| 14 | + |
| 15 | + # bad |
| 16 | + const Listing = React.createClass({ |
| 17 | + // ... |
| 18 | + render() { |
| 19 | + return <div>{this.state.hello}</div>; |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + # good |
| 24 | + class Listing extends React.Component { |
| 25 | + // ... |
| 26 | + render() { |
| 27 | + return <div>{this.state.hello}</div>; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | +* Use PascalCase for React components and camelCase for their instances. |
| 32 | + |
| 33 | + # bad |
| 34 | + import foodMenus from './FoodMenus'; |
| 35 | + |
| 36 | + # good |
| 37 | + import FoodMenus from './FoodMenus'; |
| 38 | + |
| 39 | + # bad |
| 40 | + const FoodMenus = <FoodMenus />; |
| 41 | + |
| 42 | + # good |
| 43 | + const foodMenus = <FoodMenus />; |
| 44 | + |
| 45 | +* Use the filename as the component name. For example, FoodMenus.jsx should have a reference name of FoodMenus. |
| 46 | + |
| 47 | +* Do not use displayName for naming components. Instead, name the component by reference. |
| 48 | + |
| 49 | + # bad |
| 50 | + export default React.createClass({ |
| 51 | + displayName: 'ReservationCard', |
| 52 | + // ... |
| 53 | + }); |
| 54 | + |
| 55 | + # good |
| 56 | + class ReservationCard extends React.Component { |
| 57 | + // ... |
| 58 | + } |
| 59 | + |
| 60 | +* Follow these alignment styles for JSX syntax. |
| 61 | + |
| 62 | + # bad |
| 63 | + <Foo superLongParam="bar" |
| 64 | + anotherSuperLongParam="baz" /> |
| 65 | + |
| 66 | + # good |
| 67 | + <Foo |
| 68 | + superLongParam="bar" |
| 69 | + anotherSuperLongParam="baz" |
| 70 | + /> |
| 71 | + |
| 72 | +* Use double quotes (") for JSX attributes & single quotes (') for all other JS. |
| 73 | + |
| 74 | + # bad |
| 75 | + <Foo bar='bar' /> |
| 76 | + |
| 77 | + # good |
| 78 | + <Foo bar="bar" /> |
| 79 | + |
| 80 | + # bad |
| 81 | + <Foo style={{ left: "20px" }} /> |
| 82 | + |
| 83 | + # good |
| 84 | + <Foo style={{ left: '20px' }} /> |
| 85 | + |
| 86 | +* Always include a single space in your self-closing tag. |
| 87 | + |
| 88 | + # bad |
| 89 | + <Foo/> |
| 90 | + |
| 91 | + # good |
| 92 | + <Foo /> |
| 93 | + |
| 94 | +* Use camelCase for prop names. |
| 95 | + |
| 96 | + # bad |
| 97 | + <Foo UserName="john" /> |
| 98 | + |
| 99 | + # good |
| 100 | + <Foo userName="john" /> |
| 101 | + |
| 102 | +* Omit the value of the prop when it is explicitly true. |
| 103 | + # bad |
| 104 | + <Foo hidden={true} /> |
| 105 | + |
| 106 | + # good |
| 107 | + <Foo hidden /> |
| 108 | + |
| 109 | +* Avoid using an array index as key prop, prefer a unique ID. |
| 110 | + |
| 111 | + # bad |
| 112 | + {todos.map((todo, index) => |
| 113 | + <Todo |
| 114 | + {...todo} |
| 115 | + key={index} |
| 116 | + /> |
| 117 | + )} |
| 118 | + |
| 119 | + # good |
| 120 | + {todos.map(todo => ( |
| 121 | + <Todo |
| 122 | + {...todo} |
| 123 | + key={todo.id} |
| 124 | + /> |
| 125 | + ))} |
| 126 | + |
| 127 | +* Wrap JSX tags in parentheses when they span more than one line. |
| 128 | + |
| 129 | + # bad |
| 130 | + render() { |
| 131 | + return <MyComponent className="long body" foo="bar"> |
| 132 | + <MyChild /> |
| 133 | + </MyComponent>; |
| 134 | + } |
| 135 | + |
| 136 | + # good |
| 137 | + render() { |
| 138 | + return ( |
| 139 | + <MyComponent className="long body" foo="bar"> |
| 140 | + <MyChild /> |
| 141 | + </MyComponent> |
| 142 | + ); |
| 143 | + } |
| 144 | + |
0 commit comments