Hooks
7 pages · ~5 min✓ Reviewed
Builds on State and Props. Next up: Effects and Data Fetching.
Hooks Mental Model and Rules
1. What a hook is
- A hook is a function whose name starts with
use - Lets a function component keep state and effects across renders
- React matches hooks to state by call order, not by name
2. The two rules
javascript
const [n, setN] = useState(0); useEffect(() => { if (on) save(n); }, [on, n]);
put the condition inside the hook, never around it
3. Each render is a snapshot
- Renderprops, state fixed
- Eventhandler runs
- setStateschedules render
- New rendernew values