简体   繁体   中英

Double post in react code

I am trying to make a todo app in react. But it keeps double posting. I made an edit function and after that it keeps double posting. I have followed the example of the guide. But I can not find the error.

   <!DOCTYPE html>
  <html>
  <head>
    <title>EASY SHIT</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="../../build/react.js"></script>
    <script src="../../build/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
    <style>
      body{
        margin-top:30px;
      }

      a.delete{
          color: red;
      }
      </style>
</head>
  <body>
    <div class="container">
  <div class="row">
      <div class="col-md-12">
    <div id="app"></div>
</div>
</div>
  </div>


    <script type="text/babel">
      var App = React.createClass({
        getInitialState: function (){
          return{
            text: '',
            isEdit: 0,
            todos:[
              {
                id: 1,
                text: 'meeting at work'
              },
              {
                id: 2,
                text: 'Walk the dog'
              },
              {
                id: 3,
                text: 'Eat candy'
              }
            ]
          }
        },
          render: function(){
            return(
              <div>
              <TodoForm
              {...this.state}
              changeText={this.handleChangeText}
              onTodoUpdate={this.handleTodoUpdate}
              onTodoAdd={this.handleTodoAdd} />

              <TodoList
              {...this.state}
              editTodo={this.handleTodoEdit}
              deleteTodo={this.handleTodoDelete}/>

              </div>

            )
          },
          handleTodoAdd: function(text){
            var newTodo = {
              id: this.state.todos.length + 1,
              text: text
            }

            this.setState({todos: this.state.todos.concat(newTodo)});

          },

          handleTodoDelete: function(todo){
            var todos = this.state.todos;
            for(var i = 0;i <todos.length; i++) {
              if(todos[i].id == todo.id){
                todos.splice(i, 1);
              }
            }

            this.setState({todos: todos});

          },


          handleTodoEdit: function(todo){
            this.setState({
              text: todo.text,
              isEdit: todo.id
            });

          },

          handleChangeText: function(text){
            this.setState({text: text});
          },

          handleTodoUpdate: function(todo){
            var todos = this.state.todos;
            for(var i = 0;i <todos.length; i++) {
              if(todos[i].id == todo.id){
                todos.splice(i, 1);
              }
            }
            todos.push(todo);
            this.setState({todos: todos});

          }

      });

      var TodoForm = React.createClass({
          render: function(){
            return(
              <div>
            <form onSubmit={this.onSubmit}>
              <div className="form-group">
                <label>Todo text</label>
                <input type="text" ref="text" value={this.props.text} onChange={this.onChange} className="form-control" />

              </div>
            </form>
              </div>

            )
          },

          onChange: function(e){
            this.props.changeText(e.target.value);
          },

          onSubmit: function(e){
            e.preventDefault();
            var text = this.refs.text.value.trim();

            if(!text){
              alert('Please enter something');
              return;
            }

            if(this.props.isEdit){
              var updatedTodo = {
                id:this.props.isEdit,
                text: text

              }

              this.props.onTodoUpdate(updatedTodo);
            } else {
              this.props.onTodoAdd(text);
            }

            this.props.onTodoAdd(text);
            this.refs.text.value= '';

          }

      });


      var TodoList = React.createClass({
          render: function(){
            return(
              <ul className="list-group">
            {
              this.props.todos.map(todo => {
                return <li className="list-group-item" todo={todo} key ={todo.id}>
                <span onClick={this.editTodo.bind(this, todo)}> {todo.text}</span> <a onClick={this.OnDelete.bind(this, todo)}className="delete" href="#">x</a></li>
              })
            }
              </ul>

            )
          },

          OnDelete: function(todo){
            this.props.deleteTodo(todo);
          },

          editTodo: function(todo){
            this.props.editTodo(todo);
          }

      });
      ReactDOM.render(
        <App />,
        document.getElementById('app')
      );
    </script>
  </body>
</html>

It looks to me like you aren't tracking whether you are editing an existing item or adding a new one.

If you're editing, then you need to track which one, then replace it or update it in your state, otherwise append to the list. You're just appending, so your app always thinks a new one is being added and hence it looks like it's double posting, but it is actually adding a brand new item that happens to have updated text.

The other option is to call delete and then edit, which will have the same effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM