Created
March 26, 2015 23:47
-
-
Save pauliusuza/856c53fb3496904d3c77 to your computer and use it in GitHub Desktop.
react_todo_list
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
var TodoList = React.createClass({ | |
render: function() { | |
var createItem = function(itemText) { | |
return <li>{itemText}</li>; | |
}; | |
return <ul>{this.props.items.map(createItem)}</ul>; | |
} | |
}); | |
var TodoApp = React.createClass({ | |
getInitialState: function() { | |
return {items: [], text: ''}; | |
}, | |
onChange: function(e) { | |
this.setState({text: e.target.value}); | |
}, | |
handleSubmit: function(e) { | |
e.preventDefault(); | |
var nextItems = this.state.items.concat([this.state.text]); | |
var nextText = ''; | |
this.setState({items: nextItems, text: nextText}); | |
}, | |
render: function() { | |
return ( | |
<div> | |
<h3>TODO</h3> | |
<TodoList items={this.state.items} /> | |
<form onSubmit={this.handleSubmit}> | |
<input onChange={this.onChange} value={this.state.text} /> | |
<button>{'Add #' + (this.state.items.length + 1)}</button> | |
</form> | |
</div> | |
); | |
} | |
}); | |
React.render(<TodoApp />, mountNode); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment