How call React's setState function from outside of any component, like a utility function?

后端 未结 4 1530
忘了有多久
忘了有多久 2021-01-22 08:16

I have an app where I want to move the read, write operations to a utility file i.e. away from component. I am trying to do it this way.

component.js

imp         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 08:29

    Replace the bind function with arrow function will allow it works like a callback.

    saveData((newState) => this.setState(newState), data);
    

提交回复
热议问题