React
Rules of React
A checklist of React practices for components, hooks, state updates, and rendering.
1 view
- Components return a single JSX element (or null/undefined), using a parent like
<div>or<>for multiple elements. - Components must be pure, avoiding side effects during rendering (use
useEffectfor side effects). - Update state immutably using setter functions, not direct mutations.
- Call hooks (
useState,useEffect, etc.) only at the top level of components or custom hooks, not in loops/conditionals. - Use hooks only in functional components or custom hooks, not regular functions or classes.
- Treat props as read-only; never modify them directly.
- Ensure JSX tags are properly closed (self-closing or with a closing tag).
- Use unique, stable
keyprops for list items, avoiding array indices unless static. - Optimize to prevent unnecessary re-renders with
React.memo,useMemo, oruseCallback. - Use descriptive prop names, avoiding reserved words (e.g., use
classNameinstead ofclass,htmlForinstead offor). - Name components with PascalCase and props/variables with camelCase.
- Pass event handlers as functions, not strings (e.g.,
onClick={handleClick}). - Handle side effects in
useEffectwith proper cleanup functions. - Avoid adding props to Fragments except for
keyin lists. - Define event handlers outside JSX to avoid creating new functions on each render.