Uncontrolled components

Let's start with a basic example—displaying a form with an input field and a Submit button.

The code is pretty straightforward:

  const Uncontrolled = () => (     <form>       <input type="text" />       <button>Submit</button>     </form>   );

If we run the preceding snippet in the browser, we can see exactly what we expect—an input field in which we can write something and a clickable button. This is an example of an uncontrolled component, where we do not set the value of the input field, but we let the component manage its own internal state.

Most likely, we want to do something with the value of the element when the Submit button is clicked. For example, we may want to send the data to an API endpoint.

We can do this easily by ...

Get React Design Patterns and Best Practices - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.