Created
March 3, 2017 12:28
-
-
Save johann-sonntagbauer/0849b0645864f033672374881a14cc23 to your computer and use it in GitHub Desktop.
controlled
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
class App extends React.Component { | |
constructor() { | |
super(); | |
this.state = { | |
entries: ['Stefanie', 'Sepp', 'Alex'], | |
currentValue: '' | |
} | |
} | |
updateCurrentValue = (e) => { | |
this.setState({ | |
currentValue: e.target.value | |
}) | |
} | |
render() { | |
return <div> | |
<Input value={this.state.currentValue} onChange={this.updateCurrentValue}/><Button onClick={ | |
() => { | |
this.setState({ | |
entries: [...this.state.entries, this.state.currentValue], | |
currentValue: '' | |
}) | |
} | |
}/> | |
<List entries={this.state.entries}/> | |
</div> | |
} | |
} | |
function Input({onChange, value}) { | |
return <input type="text" value={value} onChange={onChange}/> | |
} | |
function Button({onClick}) { | |
return <button onClick={onClick} | |
>+</button> | |
} | |
function List({entries}) { | |
return <div> | |
{ | |
entries.map( | |
(entry, index) => | |
<Person key={index} name={entry}/>) | |
} | |
</div> | |
} | |
function Person ({name}) { | |
return <div>Name: {name}</div> | |
} | |
Person.propTypes = { | |
name: React.PropTypes.string | |
} | |
ReactDOM.render( | |
<App /> | |
, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment