adding new objects to localstorage

前端 未结 2 1167
情歌与酒
情歌与酒 2020-12-05 14:34

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

2条回答
  •  感动是毒
    2020-12-05 15:31

    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

提交回复
热议问题