└── README.md
/README.md:
--------------------------------------------------------------------------------
1 | # React pitfalls v18.2.0
2 | Collection of react pitfalls as described in react documentation
3 |
4 | ---
5 |
6 |
7 | ## Describing the UI
8 |
9 |
10 |
11 | ## 1 - capitalize component name
12 | React components are regular JavaScript functions, but their names must start with a capital letter or they won’t work!
13 |
14 | *source: https://react.dev/learn/your-first-component#step-2-define-the-function*
15 |
16 |
17 |
18 | ## 2 - parentheses after return
19 |
20 | Without parentheses, any code on the lines after return [will be ignored](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi)!
21 |
22 | ```javascript
23 | return (
24 |
25 |
26 |
27 | );
28 | ```
29 |
30 | *source: https://react.dev/learn/your-first-component#step-3-add-markup*
31 |
32 |
33 |
34 | ## 3 - don't nest component definitions
35 |
36 | Components can render other components, but **you must never nest their definitions**:
37 |
38 | ```javascript
39 | export default function Gallery() {
40 | // 🔴 Never define a component inside another component!
41 | function Profile() {
42 | // ...
43 | }
44 | // ...
45 | }
46 | ```
47 |
48 | The snippet above is [very slow and causes bugs](https://react.dev/learn/preserving-and-resetting-state#different-components-at-the-same-position-reset-state). Instead, define every component at the top level:
49 |
50 | ```javascript
51 | export default function Gallery() {
52 | // ...
53 | }
54 |
55 | // ✅ Declare components at the top level
56 | function Profile() {
57 | // ...
58 | }
59 | ```
60 |
61 | When a child component needs some data from a parent, [pass it by props](https://react.dev/learn/passing-props-to-a-component) instead of nesting definitions.
62 |
63 | *source: https://react.dev/learn/your-first-component#nesting-and-organizing-components*
64 |
65 |
66 |
67 |
68 |
69 |
70 | ## 4 - *aria-** and *data-** keep their dash
71 |
72 | For historical reasons, [aria-*](https://developer.mozilla.org/docs/Web/Accessibility/ARIA) and [data-*](https://developer.mozilla.org/docs/Learn/HTML/Howto/Use_data_attributes) attributes are written as in HTML with dashes.
73 |
74 | *source: https://react.dev/learn/writing-markup-with-jsx#3-camelcase-salls-most-of-the-things*
75 |
76 |
77 |
78 |
79 | ## 5 - camel case for style properties
80 |
81 | Inline `style` properties are written in camelCase. For example, HTML `
` would be written as `
` in your component.
82 |
83 | *source: https://react.dev/learn/javascript-in-jsx-with-curly-braces#using-double-curlies-css-and-other-objects-in-jsx*
84 |
85 |
86 |
87 |
88 | ## 6 - curly braces in props destructuring
89 |
90 | **Don’t miss the pair of `{` and `}` curlies** inside of `(` and `)` when declaring props:
91 |
92 | ```javascript
93 | function Avatar({ person, size }) { // ...}
94 | ```
95 |
96 | This syntax is called [“destructuring”](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Unpacking_fields_from_objects_passed_as_a_function_parameter) and is equivalent to reading properties from a function parameter:
97 |
98 | ```javascript
99 | function Avatar(props) {
100 | let person = props.person;
101 | let size = props.size;
102 | // ...}
103 | ```
104 |
105 | *source: https://react.dev/learn/passing-props-to-a-component#step-2-read-props-inside-the-child-component*
106 |
107 |
108 |
109 | ## 7 - no numbers after `&&`
110 |
111 | **Don’t put numbers on the left side of `&&`.**
112 |
113 | To test the condition, JavaScript converts the left side to a boolean automatically. However, if the left side is `0`, then the whole expression gets that value (`0`), and React will happily render `0` rather than nothing.
114 |
115 | For example, a common mistake is to write code like `messageCount &&
New messages
`. It’s easy to assume that it renders nothing when `messageCount` is `0`, but it really renders the `0` itself!
116 |
117 | To fix it, make the left side a boolean: `messageCount > 0 &&
New messages
`.
118 |
119 | *source: https://react.dev/learn/conditional-rendering#logical-and-operator-*
120 |
121 |
122 |
123 |
124 | ## 8 - explicit return in arrow functions
125 |
126 | Arrow functions implicitly return the expression right after `=>`, so you didn’t need a `return` statement:
127 |
128 | ```javascript
129 | const listItems = chemists.map(person =>
130 |
...
// Implicit return!
131 | );
132 | ```
133 |
134 | However, **you must write `return` explicitly if your `=>` is followed by a `{` curly brace!**
135 |
136 | ```javascript
137 | const listItems = chemists.map(person => { // Curly brace
138 | return
...
;
139 | });
140 | ```
141 |
142 | Arrow functions containing `=> {` are said to have a [“block body”.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#function_body) They let you write more than a single line of code, but you _have to_ write a `return` statement yourself. If you forget it, nothing gets returned!
143 |
144 | *source: https://react.dev/learn/rendering-lists#filtering-arrays-of-items*
145 |
146 |
147 |
148 |
149 | ## 9 - don't use array's item index and random numbers as keys
150 |
151 | You might be tempted to use an item’s index in the array as its key. In fact, that’s what React will use if you don’t specify a `key` at all. But the order in which you render items will change over time if an item is inserted, deleted, or if the array gets reordered. Index as a key often leads to subtle and confusing bugs.
152 |
153 | Similarly, do not generate keys on the fly, e.g. with `key={Math.random()}`. This will cause keys to never match up between renders, leading to all your components and DOM being recreated every time. Not only is this slow, but it will also lose any user input inside the list items. Instead, use a stable ID based on the data.
154 |
155 | Note that your components won’t receive `key` as a prop. It’s only used as a hint by React itself. If your component needs an ID, you have to pass it as a separate prop: ``.
156 |
157 | *source: https://react.dev/learn/rendering-lists#why-does-react-need-keys*
158 |
159 | ---
160 |
161 |
162 |
163 | ## Adding Interactivity
164 |
165 |
166 |
167 | ## 10 - pass a function, not call a function
168 |
169 | Functions passed to event handlers must be passed, not called. For example:
170 |
171 | | passing a function (correct) | calling a function (incorrect) |
172 | | --- | --- |
173 | | `