Command Palette

Search for a command to run...

Hectal
Phase 10: Custom Hooks and Reusable Logic
React · Exercise 1 of 6Next
Beginner

Your first hook: useToggle

Write a custom hook useToggle(initial = false) that keeps a true/false value in state and returns [on, toggle], where toggle flips the value.

Use it in Faq: the button (id="toggle") says Show answer when closed and Hide answer when open, and the answer paragraph (id="answer") appears only when open.

What Check tests

  1. There is a hook named useToggle that uses useState
  2. At the start the answer is hidden and the button says Show answer
  3. One click shows the answer and the button says Hide answer
  4. A second click hides it again

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.