| description | React patterns for functional components in .tsx and .jsx files | ||
|---|---|---|---|
| globs |
|
- Use functional components exclusively. Never write class components.
- One component per file. The file name matches the component name (PascalCase).
- Use named exports for components. Avoid default exports except at page/route level.
- Extract shared stateful logic into custom hooks (
useprefix). - Keep hooks at the top level of a component. Never call hooks inside conditions or loops.
- Use
useCallbackanduseMemoonly when there is a measurable performance problem. Do not add them speculatively. - Use
React.memosparingly and only after profiling.
- Define prop types with a
Propsinterface in the same file, above the component. - Do not use
React.FC- annotate props directly:function MyComponent({ title }: Props). - Mark props that will not change as
readonly.
- Add
aria-labeloraria-labelledbyto interactive elements that lack visible text. - Use semantic HTML elements (
button,nav,main,section) instead of genericdivwrappers. - Ensure all images have an
altattribute.
- Prefer composition over inheritance. Use children, render props, or compound components.
- Use error boundaries around sections that may throw (data fetching, third-party widgets).
- Colocate styles with the component (CSS modules or styled components in the same directory).
- Keep state as local as possible. Lift state only when two components genuinely share it.
- Avoid putting derived data in state. Compute it during render instead.
- Use
keyprops that are stable and unique (never array indexes for reordered lists). - Avoid inline object and function literals in JSX props for frequently re-rendered components.