Clearing state es6 React

前端 未结 13 873
独厮守ぢ
独厮守ぢ 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 13:56

    First, you'll need to store your initial state when using the componentWillMount() function from the component lifecycle:

    componentWillMount() {
        this.initialState = this.state
    }
    

    This stores your initial state and can be used to reset the state whenever you need by calling

    this.setState(this.initialState)
    

提交回复
热议问题