on the homepage of React, there's the last example (A Component Using External Plugins) with a textarea
:
<textarea
id="markdown-content"
onChange={this.handleChange}
defaultValue={this.state.value}
/>
As I type, the textarea
gets updated.
Now, I tried to change defaultValue
with value
:
<textarea
id="markdown-content"
onChange={this.handleChange}
value={this.state.value}
/>
And the outcome is the same (as with defaultValue
, i.e. as I type, the textarea
gets updated visually with the updated text).
So, what is the real difference between the two?
In React, defaultValue is used with uncontrolled form components whereas value is used with controlled form components. They should not be used together in a form element.
An input form element whose value is controlled by React in this way is called a “controlled component”. For example, if we want to make the previous example log the name when it is submitted, we can write the form as a controlled component: class NameForm extends React. state = {value: ''}; this.
In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself. To write an uncontrolled component, instead of writing an event handler for every state update, you can use a ref to get form values from the DOM.
Putting a value attribute makes the input controlled, which means that the value in the input will always exactly correspond to what's in the React state, which will make the program's logic easier to reason about.
I think a good example for this is if you use a hard coded string
function App(){
return(
<textarea
defaultValue="foo" // only by default foo
/>
);
}
ReactDOM.render(<App/>, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
function App(){
return(
<textarea
value="foo" // will forever be foo
/>
);
}
ReactDOM.render(<App/>, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
So while the snippet below this paragraph might look like it is the same as using value
prop because of onChange
potentially updating the state value (therefore it looks like it is updating defaultValue
prop) - it is not. It is still an uncontrolled component and will update its value directly from the user input. It is simply initialized with the default value of whatever this.state.value
is when it is initially rendered.
<textarea
id="markdown-content"
onChange={this.handleChange}
defaultValue={this.state.value}
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With