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 ...

Get ReactJS by Example - Building Modern Web Applications with React now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.