Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing state es6 React

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());

however this does not work with the es6 class syntax.

How would I achieve the same result?

like image 517
Guy Avatar asked Jan 18 '16 00:01

Guy


People also ask

How do you clear the state in React?

Resetting States to Initial State Then we call useState in App to create the object state. Next, we create the clearState function to reset the state by calling the setState state setter function with a copy of the initialState . Making a copy lets React know that it has to update the state and re-render.

How do I reset my componentWillUnmount state?

According to people at Facebook, setState should not be called on componentWillUnmount as the component is about to be destroyed and never remounted. If you just want to "clear" the state, then there is no need for that, as any new instance of the component will have the clear initial state.

Does re render reset state React?

No, The state will remain as it is until your component unmounts. If you want to trigger something while unmounting then you can use useEffect hook.

What is reset state?

In a computer or data transmission system, a reset clears any pending errors or events and brings a system to normal condition or an initial state, usually in a controlled manner.


Video Answer


4 Answers

To the best of my knowledge, React components don't keep a copy of the initial state, so you'll just have to do it yourself.

const initialState = {
    /* etc */
};

class MyComponent extends Component {
    constructor(props) {
        super(props)
        this.state = initialState;
    }
    reset() {
        this.setState(initialState);
    }
    /* etc */
}

Beware that the line this.state = initialState; requires you never to mutate your state, otherwise you'll pollute initialState and make a reset impossible. If you can't avoid mutations, then you'll need to create a copy of initialState in the constructor. (Or make initialState a function, as per getInitialState().)

Finally, I'd recommend you use setState() and not replaceState().

like image 156
David L. Walsh Avatar answered Oct 18 '22 19:10

David L. Walsh


Problem

The accepted answer:

const initialState = {
    /* etc */
};

class MyComponent extends Component {
    constructor(props) {
        super(props)
        this.state = initialState;
    }
    reset() {
        this.setState(initialState);
    }
    /* etc */
}

unfortunately is not correct.

initialState is passed as a reference to this.state, so whenever you change state you also change initialState (const doesn't really matter here). The result is that you can never go back to initialState.

Solution

You have to deep copy initialState to state, then it will work. Either write a deep copy function yourself or use some existing module like this.

like image 42
RaptoX Avatar answered Oct 18 '22 19:10

RaptoX


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());
  }
}
like image 42
Eruz Apodaca Avatar answered Oct 18 '22 20:10

Eruz Apodaca


The solutions that involve setting this.state directly aren't working for me in React 16, so here is what I did to reset each key:

  const initialState = { example: 'example' }
  ...
  constructor() {
      super()
      this.state = initialState
  }
  ...
  reset() {
    const keys = Object.keys(this.state)
    const stateReset = keys.reduce((acc, v) => ({ ...acc, [v]: undefined }), {})
    this.setState({ ...stateReset, ...initialState })
  }
like image 14
Charles Offenbacher Avatar answered Oct 18 '22 21:10

Charles Offenbacher