Extract a reusable custom hook
Extract behavior, not markup
A custom Hook is a function that uses React Hooks to package reusable behavior. A component can call it to obtain values and actions while deciding how to display them. Start with a small feature, such as opening a field guide panel. All examples in this chapter are independent App.jsx files for the Chapter 3 client project. The described UI results are expected, not browser-tested.
import React, { useState } from 'react';function useToggle(initial = false) { const [open, setOpen] = useState(initial); function toggle() { setOpen(previous => !previous); } return { open, toggle };}export default function App() { const { open, toggle } = useToggle(); return <main><button aria-expanded={open} onClick={toggle}>Field guide</button> {open && <p>Bring a notebook and water.</p>}</main>;}The guide starts hidden; activating the button reveals it, and a second activation hides it. The Hook owns the state and the action, while App owns the markup. The updater uses the previous boolean, so it works with queued state updates. Naming a function with a use prefix followed by an uppercase letter identifies it as a Hook; naming alone does not give a plain function React state.
Keep computations separate from Hooks
import React, { useState } from 'react';function advance(step, last) { return Math.min(step + 1, last); }function useGuideStep(last) { const [step, setStep] = useState(1); return { step, next: () => setStep(s => advance(s, last)) };}export default function App() { const {step, next} = useGuideStep(3); return <main><p>Step {step} of 3</p><button disabled={step === 3} onClick={next}>Next</button></main>;}The number increases to three and then stops. In this example last is a fixed positive integer for the lifetime of the component. The ordinary advance function has no Hooks and can be called or tested without React. Do not name it useAdvance merely to make it look like part of React. A reusable calculation does not need a Hook.
Call the Hook consistently
import React, { useState } from 'react';function useGuideNote() { return useState(''); }function Guide({visible}) { if (!visible) return <p>Guide hidden</p>; const [note, setNote] = useGuideNote(); return <label>Note <input value={note} onChange={e => setNote(e.target.value)} /></label>;}export default function App() { const [visible, setVisible] = useState(true); return <main><button onClick={() => setVisible(v => !v)}>Toggle guide</button><Guide visible={visible} /></main>;}This is valid JSX but violates the Hook calling rules: Guide skips a Hook on some renders. Move the useGuideNote call above the early return to preserve the note when hidden, or render a separate NoteEditor conditionally to reset its state when removed. Do not execute the incorrect example as a normal lab. The special React use API has different calling rules; that exception does not apply to a custom Hook. Practice: repair the first approach and predict the result after hiding and showing a typed note.