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

后端 未结 4 1507
忘了有多久
忘了有多久 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:33

    Need to bind this to setState.

     saveData(this.setState.bind(this), data);
    

    Working codesandbox demo

提交回复
热议问题