Controlled components
"What is a controlled component, Mike?" asked Shawn.
"It's an input component whose value is controlled by React. By providing the value prop, we are informing React that the value of this field is "Shawn". Once React has declared it to be "Shawn", any user input won't have an effect as the value is already set in the ." explained Mike.
"I think that we have to resort to state
instead of props
?" Shawn asked.
"Exactly. As an user is interacting with the input field, we need to update the value prop
using state
and onChange
event handler. Can you give it a try?" suggested Mike.
// src/index.js var InputExample = React.createClass({ getInitialState() { return ( { name: '-'} ); }, handleChange(event) { this.setState({ name: event.target.value }); }, render() { return ( <input type="text" value={this.state.name} onChange={this.handleChange} /> ); } });
"Awesome. This pattern of making the value of...