Hint
Only call at top level, only in React functions — React relies on call order to track state
Two rules enforced by the eslint-plugin-react-hooks linter:
Why? React tracks hook state by call order. It uses an internal linked list — hook #1, hook #2, etc. If you call hooks conditionally, the order changes between renders and React's state gets mismatched:
// ❌ WRONG — conditional hook breaks the call order
function Profile({ userId }) {
if (!userId) return null; // return before hook!
const [user, setUser] = useState(null); // hook #1 sometimes skipped
}
// ✅ CORRECT — always call hooks, handle condition inside
function Profile({ userId }) {
const [user, setUser] = useState(null); // hook #1 always
if (!userId) return null; // condition after hooks
}