└── 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 | Katherine Johnson 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 `