Do not mutate state directly, Use setState() react/no-direct-mutation-state in React JS

后端 未结 2 793
北恋
北恋 2021-01-17 17:07
 { this.state.name = input; }}
  name=\"name\"
  type=\"text\"
  className=\"form-control\"
  on         


        
2条回答
  •  攒了一身酷
    2021-01-17 17:35

    you can instead clone the entire property value inside the with spread operator and then reform or edit the value for example :

    state = {Counters: [{id:1,value:1},{id: 2,value: 2},{id: 3,value: 3},{id: 4,value: 4}]}
    increment = (Counter) => {
    
            //This is where the state property value is cloned 
    
            const Counters = [...this.state.Counters];
            console.log(Counters);
            const index = Counters.indexOf(Counter)
            Counters[index].value++
            this.setState({
                Counters: this.state.Counters
            })
    }
    

提交回复
热议问题