I\'m trying to make shopping cart front end with localstorage, as there are some modal windows and I need to pass cart items info there. Every time you click add to cart it
it's not directly related to local storage but nowdays, it's a good practice to use React/Angular. here is a example:
var TodoItem = React.createClass({
done: function() {
this.props.done(this.props.todo);
},
render: function() {
return - {this.props.todo}
}
});
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 (
Todos: {this.props.todos.length}
{
this.state.todos.map(function(todo) {
return
}.bind(this))
}
);
}
});
var todos = JSON.parse(localStorage.getItem('todos')) || [];
React.render(
,
document.getElementById('container')
);
from here