React TODO app localStorage
by Kensuke Nagae
May 20, 2015
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/react-with-addons.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
CSS
li {
cursor: pointer;
}
JavaScript 1.7
var TodoItem = React.createClass({
done: function() {
this.props.done(this.props.todo);
},
render: function() {
return <li onClick={this.done}>{this.props.todo}</li>
}
});
var TodoList = React.createClass({
getInitialState: function() {
return {
todos: this.props.todos
};
},
add: function() {
var todos = this.props.todos;
todos.push(React.findDOMNode(this.refs.myInput).value);
React.findDOMNode(this.refs.myInput).value = "";
localStorage.setItem('todos', JSON.stringify(todos));
this.setState({ todos: todos });
},
done: function(todo) {
var todos = this.props.todos;
todos.splice(todos.indexOf(todo), 1);
localStorage.setItem('todos', JSON.stringify(todos));
this.setState({ todos: todos });
},
render: function() {
return (
<div>
<h1>Todos: {this.props.todos.length}</h1>
<ul>
{
this.state.todos.map(function(todo) {
return <TodoItem todo={todo} done={this.done} />
}.bind(this))
}
</ul>
<input type="text" ref="myInput" />
<button onClick={this.add}>Add</button>
</div>
);
}
});
var todos = JSON.parse(localStorage.getItem('todos')) || [];
React.render(
<TodoList todos={todos} />,
document.getElementById('container')
);