Bootcamp Notes – Day 12 (Tues) – React: Week 4 – Uncontrolled Forms
Uncontrolled Forms
Uncontrolled Components
Controlled VS Uncontrolled
Controlled forms tie form component values directly to the state of the parent component. They require a lot of initial setup. but are the recommended way to set up form in React. But there is another way to set up forms using Uncontrolled components. Uncontrolled components are an alternative – quicker to set up, easier if you are trying to integrate React code with non-React code. It is a quick and dirty way to create a small form with not much code or if you are trying to integrate React Code with non-React code. For example, you might work on a project that was built without using React, then later wish to add React components to it. Then using uncontrolled components for forms can make that integration easier.
Uncontrolled Forms
So how do uncontrolled forms work? HTML forms naturally keep their own internal state, tacked by the DOM, separate from React app.
Controlled forms: React controls form state.
Uncontrolled forms: DOM continues to handle the form state, React just retrieves the date when needed – the form elements within the DOM act as the single source of truth.
We can use refs (short for reference) to retrieve the data – in Reactstrap, called innerRef.
Exercise: Uncontrolled Forms
HeaderComponent.js
Additional Resources:
- React – Uncontrolled Components
- Medium.com – Controlled and Uncontrolled Input Values in React
- Controlled and Uncontrolled form inputs in React
- Reactstrap – Modal