Controlled and Uncontrolled components
Last updated
Last updated
Uncontrolled Components – React Official docs https://reactjs.org/docs/uncontrolled-components.html
Controlled Components – React Official docs https://reactjs.org/docs/forms.html#controlled-components
Controlled and uncontrolled form inputs in React don't have to be complicated https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/
An uncontrolled component keeps the source of truth in the DOM. A good example for this, are input
s.
Take this example for instance:
Here, the code relies of the value of the input in the DOM rather than in its component state.
In most cases, it's recommend to use controlled components to implement forms.
In a controlled component, the data are handled by the React component and is usually stored in its state.
Here is the same above example with a controlled component:
Take the time to look at the other example of the React official documentation:
Textarea tag
Handling multiple inputs
https://reactjs.org/docs/forms.html#handling-multiple-inputs