Created
April 22, 2019 14:15
-
-
Save BolajiAyodeji/5c428da996eba386add9b681066b13da to your computer and use it in GitHub Desktop.
This file contains 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
class TodoApp extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { items: [], text: '' }; | |
this.handleChange = this.handleChange.bind(this); | |
this.handleSubmit = this.handleSubmit.bind(this); | |
} | |
render() { | |
return ( | |
<div> | |
<h3>TODO</h3> | |
<TodoList items={this.state.items} /> | |
<form onSubmit={this.handleSubmit}> | |
<label htmlFor="new-todo"> | |
What needs to be done? | |
</label> | |
<input | |
id="new-todo" | |
onChange={this.handleChange} | |
value={this.state.text} | |
/> | |
<button> | |
Add #{this.state.items.length + 1} | |
</button> | |
</form> | |
</div> | |
); | |
} | |
handleChange(e) { | |
this.setState({ text: e.target.value }); | |
} | |
handleSubmit(e) { | |
e.preventDefault(); | |
if (!this.state.text.length) { | |
return; | |
} | |
const newItem = { | |
text: this.state.text, | |
id: Date.now() | |
}; | |
this.setState(state => ({ | |
items: state.items.concat(newItem), | |
text: '' | |
})); | |
} | |
} | |
class TodoList extends React.Component { | |
render() { | |
return ( | |
<ul> | |
{this.props.items.map(item => ( | |
<li key={item.id}>{item.text}</li> | |
))} | |
</ul> | |
); | |
} | |
} | |
ReactDOM.render( | |
<TodoApp />, | |
document.getElementById('todos-example') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment