← Cheat Sheets
JSX Rules and Patterns
Syntax rules that trip people up.
# JSX Rules and Patterns
Syntax rules that trip people up.
- Return **one root** element or a fragment `<>...</>`
- Use `className` and `htmlFor` instead of `class` and `for`
- Props and attributes are camelCase: `onClick`, `tabIndex`
- Embed JavaScript in braces: `{count + 1}`
- Lists need a stable `key`: `items.map(i => <li key={i.id}>{i.name}</li>)`
- Close all tags: `<img />`
- Conditional: `{ok && <Panel />}` or `{ok ? <A /> : <B />}`
- Inline style takes an object: `style={{ color: "red" }}`
- State updates are async; use the updater form when based on previous state: `setN(n => n + 1)`
- Never mutate state directly; create a new array or object