Command Palette

Search for a command to run...

Hectal
Phase 8: Refs and the DOM
React · Exercise 4 of 6 PreviousNext
Intermediate

Expose only focus and clear

SearchBox forwards its ref to the <input>, so the parent gets the whole DOM node. Change it so the parent's ref gets only an object with two methods: focus() and clear().

clear() empties the input and focuses it. Keep the input's id="q".

Use useImperativeHandle. The parent's Clear button (id="clear") and Peek button (id="peek", which logs what the parent can see) are already written: don't change App.

What Check tests

  1. The search box starts with old search
  2. Clear empties the box without an error
  3. Clear also focuses the box
  4. The parent can no longer reach the DOM node
  5. Uses useImperativeHandle

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.