allow typescript compiler to call setState on only one react state property

前端 未结 5 2085
半阙折子戏
半阙折子戏 2020-12-13 06:17

I\'m using Typescript with React for a project. The Main component gets passed state with this interface.

interface MainState {
  todos: Todo[];
  hungry: B         


        
5条回答
  •  一个人的身影
    2020-12-13 06:43

    Edit: DO NOT USE this solution, prefer https://stackoverflow.com/a/41828633/1420794 See comments for details.

    Now that spread operator has shipped in TS, my preferred solution is

    this.setState({...this.state, editorState}); // do not use !
    

提交回复
热议问题