React setState + Where does 'prevState' come from?

前端 未结 2 907
孤独总比滥情好
孤独总比滥情好 2021-02-02 05:47

I just started learning React and JavaScript.
While going through the tutorial, I got to this example code of a component, which creates a toggle button.
This is part of

2条回答
  •  面向向阳花
    2021-02-02 06:33

    Im use this. (Example)

    const [modal, setModal] = useState(false);
    const [dataAction, setDataAction] = useState({name: '', description: ''});
    
    const _handleChangeName = (data) => {
        if(data.name)
            setDataAction( prevState  => ({ ...prevState,   name : data.name }));
        if(data.description)
            setDataAction( prevState  => ({ ...prevState,   description : data.description }));
      };
    

提交回复
热议问题