Why: React tracks hooks by their call index. If a hook is conditionally skipped, all subsequent hooks shift positions, causing React to return the wrong state for each hook.
Rule 2: Only Call Hooks from React Functions
Call hooks only from:
React function components
Custom hooks (functions starting with use)
// BAD — hook in a regular functionfunction getUser() { const [user] = useState(null); // not a component or hook! return user;}// GOOD — custom hookfunction useUser() { const [user, setUser] = useState(null); useEffect(() => { fetchUser().then(setUser); }, []); return user;}