Skip to content

Instantly share code, notes, and snippets.

@fourcolors
Created June 28, 2014 00:52
Show Gist options
  • Save fourcolors/d6c04b691ca4e44f5c4f to your computer and use it in GitHub Desktop.
Save fourcolors/d6c04b691ca4e44f5c4f to your computer and use it in GitHub Desktop.
Finished ToDoList in React
<!DOCTYPE html>
<html>
<head>
<script src="http://fb.me/react-0.10.0.js"></script>
<script src="http://fb.me/JSXTransformer-0.10.0.js"></script>
</head>
<body>
<div id="component"></div>
<script type="text/jsx">
/** @jsx React.DOM */
var ToDoAddItem = React.createClass({
addToDoItem: function(){
this.props.addItemToList({completed: false, text: this.refs.commentInput.getDOMNode().value.trim()});
this.refs.commentInput.getDOMNode().value = '';
return false;
},
render: function(){
return(
<form onSubmit={this.addToDoItem}>
<input ref="commentInput" />
<button>Add Item</button>
</form>
);
}
});
var ToDoList = React.createClass({
addItemToList: function(item){
var newList = this.state.todos;
newList.push(item);
this.setState({todos: newList});
},
getInitialState: function(){
return {todos: [{completed: false, text: 'go to the store'}, {completed: false, text: 'buy a bike'}]};
},
render: function(){
return (
<div>
<h1>ToDoList</h1>
<ToDoAddItem
addItemToList={this.addItemToList}
/>
<ul>
{this.state.todos.map(function(todo){
return <li>{todo.text}</li>;
})}
</ul>
</div>
);
}
});
React.renderComponent(<ToDoList />, document.getElementById('component'));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment