Lesson 20 — Custom hooks
Write your own hook
A custom hook is a normal function whose name starts with use and that calls other hooks inside. It packages stateful logic so any component can reuse it. Each component that calls the hook gets its own copy of the state.
function useCounter(start) {
const [n, setN] = useState(start);
return [n, () => setN((x) => x + 1)];
}
Why it matters: toggles, form fields and data loading become one-line hooks instead of repeated code.
Finish useToggle. Details should show/hide "Here are the details", and Theme should switch dark → light.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based React Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for React Foundations books — check the publication year so the examples match current versions.
Search Amazon →