Home
← All questions

Custom hooks — design patterns and rules in React

MediumPremiumReact

Asked at Meta · Airbnb · Amazon

React interview practice: Extract reusable stateful logic with custom hooks

Answer

Custom hooks are functions starting with use that compose built-in hooks to share stateful logic (not state itself — each call site gets its own state).

Rules of Hooks: only call hooks at the top level of React functions or custom hooks; only from React components or other hooks.

Patterns:

  • useX data fetching — loading/error/data + abort on unmount.
  • useDebouncedValue — encapsulate debounce + cleanup.
  • useLocalStorage — sync state with storage.
  • usePrevious — track prior prop/state for diffs.
Premium question

Unlock the full model answer with Mentoxis Premium.

Unlock with Premium