Destructuring state/props in React

前端 未结 3 1095
既然无缘
既然无缘 2021-02-06 11:54

I\'m learning React and I have Eslint installed in my project. It started giving me errors like

Use callback in setState when referencing the previous state. (r         


        
3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 12:43

    This is problem with your onChange method. Try something like this:

    onChange = e =>
        this.setState(prevState => ({
            data: { ...prevState.data, [e.target.name]: e.target.value }
        }));
    

    And look at section "State Updates May Be Asynchronous" from https://reactjs.org/docs/state-and-lifecycle.html

提交回复
热议问题