Today I Learned

Use form controls in search

November 12, 2023

search form

When building a search form, we should wrap our form controls in a <form> tag. This way, instead of listening for clicks and keys, we can listen for the form submit event.

The form submit event will be called automatically when the user clicks the button, or presses “Enter” whenever the input or button is focused. When that event fires, we’ll run our search.

<form
  onSubmit={event => {
    event.preventDefault();
    ...
  }}
>
  <input type="text" value={...} onChange={...} />
  <button>Search</button>
</form>

© 2025 - Written by Vuong Vu. Connect with me on LinkedIn.