If set value to TextField, can't change it anymore
Problem description
If you set a value in a TextField, by its property 'value', then, as a user you can't write on that TextField, for example:
<TextField id={Math.random().toString()} ref={'usernameTf'} value={this.state.profile.username} />
I'm pretty sure you're asking to yourself, why use the value property and not the defaultValue, well, the point is defaultValue does not work when you use info from state.
Steps to reproduce
- Create a TextField.
- Put a value on it, using the property value.
- ???
Versions
- Material-UI: 0.15.4
- React: 15
- Browser: Chrome 53, Opera 40.
@Solitaryo: that's because <TextField /> only follows whatever is on the state.
To use state, you should provide an onChange() function prop so it changes the state value on user typing, something like:
<TextField
value={this.state.myField.itsValue}
onChange={(e) => this.setState({ myField: e.target.value })}
...
/>
A good tip is using something like formsy-react and its Material UI wrapper by @mbrookes.
@Solitaryo You are controlling the component. That's the expected behavior. It's the same than with a native <input /> component. I'm closing this issue as it's linked with some core concept of React.