Skip to main content

3.23 Adding Two Way Binding

Dynamically Updating & Handle Inputs

App.js
  nameChangedHandler = (event) => {
this.setState({
persons: [
{ name: 'Max', age: 28 },
{ name: event.target.value, age: 29 },
{ name: 'Stephanie', age: 26 },
],
})
}

render() {
return (
<div className="App">
<h1>Hi, I'm a React app!</h1>
<p>This is really working!</p>
<button onClick={() => this.switchNameHandler('MAx')}>
Switch Name
</button>
<Person
name={this.state.persons[0].name}
age={this.state.persons[0].age}
/>
<Person
name={this.state.persons[1].name}
age={this.state.persons[1].age}
click={this.switchNameHandler.bind(this, 'Max!')}
changed={this.nameChangedHandler}
>
Person.js
const person = (props) => {
return (
<div>
<p onClick={props.click}>
I'm {props.name} and I am {props.age} years old!
</p>
<p>{props.children}</p>
<input type="text" onChange={props.changed} value={props.name} />
</div>
)
}