0

I am looking at the docs of the react.js and found that input tag with a value cannot be interactive, has to be dealt with the help of state. But there is something called uncontrolled input which need to be dealt with the state. So defaultValue should be used in the uncontrolled input and we can get the value by ref?

<input type="text" value="hello" /> 
<input type="text" defaultValue="Hello"/> 

Please clear my inference on when to use the defaultValue specifically. What is the use case for it

gates
  • 4,465
  • 7
  • 32
  • 60
  • According to https://facebook.github.io/react/docs/forms.html#default-value `defaultValue` is only used for initial rendering. Interactions by user will have to go through the usual controlled components – mfirry Jul 23 '15 at 07:58
  • Then what is the point of having defaultValue, as value does the same thing when one has to use controlled components! – gates Jul 23 '15 at 08:00
  • use `onChange={this.handleChange}` and then in `handleChange(event)` you can access the current value of the input box by `event.target.value`. You would use the state to keep track of your value, so you can access it any time. if initially your state will be set it would work as a `defaultValue` – knowbody Jul 23 '15 at 08:00
  • value pretty much does the same thing @knowbody – gates Jul 23 '15 at 08:01
  • yeah, as @mfirry mentioned `defaultValue` is used only for the initial render – knowbody Jul 23 '15 at 08:02
  • here is the answer for your question http://stackoverflow.com/questions/30146105/react-input-defaultvalue-doesnt-update-with-state – knowbody Jul 23 '15 at 08:03

0 Answers0