Can you explain and compare all hooks in React? I want a memory note to remind me every time.Easy! Every hook is a carrot in the stack. Six groups, one tall tower, then the tricky questions.
|
Which hooks manage state?const [count, setCount] = useState(0);useState is one simple variable. useReducer handles complex logic. useActionState waits for the server. useOptimistic guesses instantly.
|
What about context and refs?const theme = useContext(ThemeContext); const inputRef = useRef(null);useContext grabs global data without prop drilling. useRef holds a value or a DOM node, and changing it never re-renders.useImperativeHandle lets a child choose what the parent can touch.
|
And effect hooks?useEffect(() => { ... return () => cleanup(); }, []);useEffect runs after paint. useLayoutEffect runs before paint to stop flicker. useInsertionEffect injects CSS early.
|
Which hooks make things faster?const total = useMemo(() => heavySum(items), [items]); const onSave = useCallback(() => save(), []);useMemo caches a result. useCallback caches a function. useTransition and useDeferredValue keep typing smooth while heavy work lags behind.
|
Anything else?const id = useId();useId gives stable unique IDs. useSyncExternalStore safely reads outside stores. useDebugValue labels custom hooks in DevTools.
|
Any rules I must remember?Rule one: call hooks only at the top level, never in loops or conditions. React needs the same stack order every render.Rule two: call hooks only in components or custom hooks.
|
useActionState waits for the server and useOptimistic does it blindly, then corrects after. Is that right?Exactly. useActionState is the patient waiter and useOptimistic is the optimistic guesser.Use the waiter for bank transfers. Use the guesser for likes and chat. Combine them for complex async state.
|
Why does useEffect return a function? Doesn't return exit immediately?useEffect(() => { window.addEventListener('resize', checkMobile); return () => window.removeEventListener('resize', checkMobile); }, []);The return hands React a sealed envelope. It stays in React's pocket and opens only on unmount.
|
What is the order of events?Mount, run the effect, attach the listener, hand the cleanup to React, then run it on unmount.
|
Is useEffect the same as useCallback? I see const checkWinner = useEffect...// BUG const checkWinner = useEffect(() => { ... }, [players]);useEffect returns undefined, so checkWinner is not a function. useEffect runs automatically. useCallback only caches a function you call yourself.
|
What if playSound is missing from the dependency array?const dealCards = useCallback(() => { ... playSound('DEAL'); }, [shuffle, playSound]);No crash. dealCards gets frozen with the first playSound it saw, a stale closure. If playSound changes later, dealCards never notices.
|
When should I use useState, and when useReducer?setCount(prev => prev + 1);
const [state, dispatch] = useReducer(cartReducer, initialState); dispatch({ type: 'ADD_ITEM', payload: item });useState for simple values. Use prev => prev + 1 when the next value depends on the last one.useReducer for complex state with many actions. For a single toggle it is just extra boilerplate.
|
Now you understand hooks! Keep stacking those carrots.
|