Apply the Rules of Hooks
Keep ordinary Hook calls in a consistent structure
Hooks such as useState give a function component access to React-managed capabilities. React associates ordinary Hook calls with their order within the component. Keep them at the top level, before conditional returns, and call them from function components or custom Hooks, not ordinary helpers, event callbacks or loops. A custom Hook is a function that uses Hooks to share behavior; it starts with use followed by an uppercase letter. JSX lets React manage component identity; avoid calling component functions yourself.
import { useState } from 'react';function RoomDetails({ open }) { if (!open) return <p>Room closed</p>; const [note, setNote] = useState(''); // Incorrect Hook placement return <label>Note <input value={note} onChange={e => setNote(e.target.value)} /></label>;}export default function App() { const [open, setOpen] = useState(false); return <main><button type="button" onClick={() => setOpen(v => !v)}>Toggle room</button> <RoomDetails open={open} /></main>;}This anti-pattern is valid JSX syntax but violates the Hooks rules: the state call is skipped on one path. Do not rely on a particular diagnostic or a successful first render. Inspect the conditional return before blaming React or the bundler. This fixture is for code review, not a recommended runnable example.
import { useState } from 'react';function RoomDetails({ open }) { const [note, setNote] = useState(''); if (!open) return <p>Room closed</p>; return <label>Note <input value={note} onChange={e => setNote(e.target.value)} /></label>;}export default function App() { const [open, setOpen] = useState(false); return <main><button type="button" onClick={() => setOpen(v => !v)}>Toggle room</button> <RoomDetails open={open} /></main>;}Open, type a note, close and reopen: the note should remain because RoomDetails stays mounted. If the requirement is to discard the note when closing, the parent can conditionally mount a separate component that calls its Hooks unconditionally. Those are different state lifetime decisions, not exceptions to the rule.
Extract reusable behavior into a custom Hook
import { useState } from 'react';function useToggle(initial = false) { const [active, setActive] = useState(initial); return [active, () => setActive(v => !v)];}function RoomSwitch({ name }) { const [active, toggle] = useToggle(); return <button type="button" aria-pressed={active} onClick={toggle}>{name}</button>;}export default function App() { return <main><h1>Room controls</h1><RoomSwitch name="Quiet mode" /> <RoomSwitch name="Reading light" /></main>;}The switches start unpressed. Activating Quiet mode changes its pressed state without changing Reading light. A custom Hook shares a pattern, not the state instances themselves. Never move the useState call into toggle: that callback runs on interaction, outside component rendering. Use the official eslint-plugin-react-hooks rules in a reviewed project; syntax transformation alone cannot detect these violations.
One documented exception: React 19 use
The React 19 API named use reads a context or a Promise and is not an ordinary Hook. It permits conditional and loop calls within components or Hooks, but not calls from arbitrary event handlers; do not wrap it in try/catch. Context, Suspense and cached Promises have their own prerequisites, taught later. This exception does not apply to useState, useEffect or custom Hooks. Do not use it to bypass the ordinary rules.
Practice
Fix a component that calls useState only when a filter is enabled. Choose whether the input should retain its state while hidden. Explain how your placement of the component and the Hook implements the decision.