Clearing state es6 React

前端 未结 13 875
独厮守ぢ
独厮守ぢ 2020-12-07 13:04

I am trying to clear a components state but can\'t find a reference for the es6 syntax. I was using:

this.replaceState(this.getInitialState());

13条回答
  •  醉话见心
    2020-12-07 14:04

    This is the solution implemented as a function:

    Class MyComponent extends React.Component {
      constructor(props) {
        super(props);
        this.state = this.getInitialState();
      }
    
      getInitialState = () => ({
        /* state props */
      })
    
      resetState = () => {
         this.setState(this.getInitialState());
      }
    }
    

提交回复
热议问题