Core hooks
const [count, setCount] = useState(0);
setCount(c => c + 1); // functional update for derived state
useEffect(() => {
const id = setInterval(tick, 1000);
return () => clearInterval(id); // cleanup on unmount / dep change
}, [tick]);
const ref = useRef(null); // mutable, no re-render; DOM refs
const value = useMemo(() => expensive(a), [a]);
const onClick = useCallback(() => doIt(id), [id]);
const ctx = useContext(ThemeContext);
const [state, dispatch] = useReducer(reducer, init);Rules of hooks
- Call hooks only at the top level (no loops, conditions, nested functions).
- Call them only from components or custom hooks (
useX). - Effects run after paint;
useLayoutEffectruns before paint (measure DOM). - In dev StrictMode, effects mount → unmount → mount to expose missing cleanup.
Rendering
- A component re-renders when its state changes, its parent re-renders, or a context it uses changes.
- Reconciliation: diff the new tree against the previous one; same type at same position = update, else remount.
- Keys identify list items. Use stable ids, never array index for reorderable lists.
- State updates are batched (React 18+), even in timeouts and promises.
Performance
| Tool | Use for |
|---|---|
React.memo(C) | skip re-render if props are shallow-equal |
useMemo | cache expensive computed values |
useCallback | stable function identity for memoized children |
lazy + Suspense | code-split routes and heavy components |
useTransition / useDeferredValue | keep input responsive during heavy updates |
| virtualization (react-window) | long lists |
Measure first with React DevTools Profiler.
Custom hook
function useFetch(url) {
const [state, set] = useState({ loading: true });
useEffect(() => {
const ac = new AbortController();
fetch(url, { signal: ac.signal })
.then(r => r.json())
.then(data => set({ data }))
.catch(error => { if (error.name !== 'AbortError') set({ error }); });
return () => ac.abort(); // avoid race / setState after unmount
}, [url]);
return state;
}Controlled forms
function Form() {
const [email, setEmail] = useState('');
return (
<form onSubmit={e => { e.preventDefault(); save(email); }}>
<input value={email} onChange={e => setEmail(e.target.value)} />
</form>
);
}Controlled = React owns the value. Uncontrolled = DOM owns it, read via ref.
State management
- Local state first; lift state up to the closest common parent.
- Context for low-frequency global values (theme, user); every consumer re-renders on change.
- Server state: TanStack Query / SWR (cache, dedupe, revalidate).
- Client global state: Zustand, Redux Toolkit.
Error boundaries
class Boundary extends React.Component {
state = { error: null };
static getDerivedStateFromError(error) { return { error }; }
componentDidCatch(error, info) { report(error, info); }
render() { return this.state.error ? <Fallback /> : this.props.children; }
}They don't catch errors in event handlers or async code.
Micro-frontends
- Split a large app by business domain; each team builds & deploys independently.
- Integration: Module Federation (runtime), single-spa, iframes, web components.
- Share: design system, auth, routing shell. Pin shared libs (React) as singletons.
- Trade-offs: independent releases vs. bundle duplication, version drift, UX consistency.
React 18/19 & Next.js
createRoot, concurrent rendering, automatic batching,Suspensefor data.- Server Components render on the server, send no JS; mark interactive ones
'use client'. - Actions /
useActionState/useOptimistic(19) for form mutations. - Rendering modes: CSR, SSR, SSG, ISR. Hydration attaches handlers to server HTML.