Skip to content
Learn Netverks

Lesson

Step 31/36 86% through track

custom-hooks

Custom hooks

Last reviewed May 28, 2026 Content v20260528
Track mode
client_react
Means
In-browser React TSX
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Custom hooks: the concepts, APIs, and habits you need before advancing in React.

Hooks encode reusable stateful logic; senior reviews focus on whether you reached for the right hook.

You will apply Custom hooks in contexts like: SPAs, dashboards, design-system-driven products, and React Native mobile apps.

Write TypeScript/TSX, click Run in browser—React 18 loads from CDN, JSX compiles in the tab, UI renders in the preview root, and printOutput feeds the terminal.

Toward the end of the track—consolidate before capstone-style review lessons.

A custom hook is a function whose name starts with use and that may call other hooks. It extracts reusable stateful logic—form fields, media queries, debounced search—without inheritance or HOC wrappers.

Rules

  • Only call hooks at the top level of the custom hook (and React components)
  • Return whatever API fits: values, setters, dispatch, refs
  • Keep hooks focused; compose smaller hooks instead of one “god hook”

Example pattern

function useCounter(start = 0) {
  const [n, setN] = React.useState(start);
  const inc = () => setN((c) => c + 1);
  const reset = () => setN(start);
  return { n, inc, reset };
}

Testing hooks is easier when logic lives in a hook tested via @testing-library/react renderHook utilities.

Self-check

  1. Why must the function name start with use?
  2. What belongs in a hook vs a plain utility function?

Challenge

Extract useToggle

  1. Copy the toggle logic into function useToggle(initial).
  2. Use it in App and confirm the button still works.

Done when: toggle behavior unchanged after extraction.

Challenge

Rename to useFormField

  1. Extract value + onChange into useFormField(initial).
  2. Wire two inputs with two hook instances.

Done when: both fields update independently with shared hook logic.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Hook you extracted?
  • Naming use* rule?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump