Uncontrolled components in React are those where form data is handled by the DOM itself, instead of being managed by the component's state. To create an uncontrolled component, you follow these steps:
Use the defaultValue
Prop: Instead of using value
to set the initial value of a form element (which would make it controlled), you use defaultValue
. For example, for an input field, you would do:
<input type="text" defaultValue="Initial Value" />
Accessing Data via Refs: Since the data is not managed by React's state, you need a way to access the current value when needed. You use React's ref
system to create a reference to the DOM node. Here's how you do it:
class UncontrolledForm extends React.Component { constructor(props) { super(props); this.inputRef = React.createRef(); } handleSubmit = (event) => { event.preventDefault(); console.log('Input Value:', this.inputRef.current.value); } render() { return ( <form onSubmit={this.handleSubmit}> <input type="text" defaultValue="Initial Value" ref={this.inputRef} /> <button type="submit">Submit</button> </form> ); } }
By following these steps, you can create an uncontrolled component in React, where the form data is handled directly by the DOM.
Using uncontrolled components in React applications comes with several benefits:
To access the value of an uncontrolled component in React, you typically use the ref
system. Here's how you can do it:
Create a Ref: In your component, create a ref using React.createRef()
or the useRef
hook in functional components.
// In a class component constructor(props) { super(props); this.inputRef = React.createRef(); } // In a functional component const inputRef = useRef(null);
Attach the Ref to the DOM Element: When rendering your form element, pass the ref
to it.
// Class component <input type="text" defaultValue="Initial Value" ref={this.inputRef} /> // Functional component <input type="text" defaultValue="Initial Value" ref={inputRef} />
Access the Value: You can access the value of the uncontrolled component when needed, such as on form submission or any other event. Use ref.current.value
to get the current value.
// In a class component handleSubmit = (event) => { event.preventDefault(); console.log('Input Value:', this.inputRef.current.value); } // In a functional component const handleSubmit = (event) => { event.preventDefault(); console.log('Input Value:', inputRef.current.value); }
By following these steps, you can effectively access the value of an uncontrolled component in React.
Choosing between controlled and uncontrolled components depends on your project's specific needs and complexity. Here are some scenarios where you might prefer uncontrolled components:
In general, if you need more control over form data and its validation, controlled components are the better choice. However, for simpler or performance-critical scenarios, uncontrolled components can be a good fit.
The above is the detailed content of How do you create uncontrolled components in React?. For more information, please follow other related articles on the PHP Chinese website!