Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear out state variables in React?

Tags:

reactjs

Since support for replaceState is removed and setState does shallow merging, is it possible to remove all state variables and update the state by adding new variables as in the cases below without mutating(this.state) the state directly?

// let's say this.state is { foo: "123", bar: "456", baz: "789" }

// case 1 - this.state is cleared out, all state variables are removed
// this.state is {}

// case 2 - this.state is cleared out and new variable(s) introduced
// this.state is { qux: "lately introduced state variable" }
like image 505
Kartal Erkoc Avatar asked Dec 25 '17 21:12

Kartal Erkoc


People also ask

How do you clear the state variable 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 you reset state in React after submit?

To clear input values after form submit in React: Store the values of the input fields in state variables. Set the onSubmit prop on the form element. When the submit button is clicked, set the state variables to empty strings.

How do you clear a component in React?

The best way to remove a component in React. js is to render it conditionally using ternary operators and state. We want to toggle this text component to display based on a button click.


2 Answers

is it possible to remove all state variables and update the state by adding new variables as in the cases below without mutating(this.state) the state directly

I could be wrong, but I don't think this is possible.

When you set up state for your component, you're establishing a model for your component to adhere to, so wiping out the attributes of your components state (rather than updating them) breaks the principles of state management in react. I believe this is in part the motivation for removing replaceState

see https://github.com/facebook/react/issues/3236 for more information

Think about the difference between ice and water (the key difference being temperature). Temperature never disappears from the equation, it just changes value, and the water responds accordingly. Stateful react components operate under similar principles.

By establishing a model for your component we create a contract between the 2 and can make changes to our UX in a predictable manner.

You can of course update these values to be null, undefined, 0, false, etc but you cannot remove them from the signature of your initial state once it is created.

You could easily accomplish this with nested state however:

this.state = { foo: { bar: { baz: 'buzz' } } }

// remove state.foo.bar
this.setState({ foo: {} })
like image 66
lfender6445 Avatar answered Oct 03 '22 07:10

lfender6445


Well, no. As far as I know you are not able to go around this but with this.state = {} as you already know. However, I can't imagine case where you want to clear your state completely, but I can imagine you want to reset the state sometimes. This could be done by following

class Cmp extends React.Component {
  getClearState() {
    return {
      foo: undefined;
      bar: undefined;
      baz: undefined;
      qux: undefined;
    }
  }

  caseOne() {
    this.setState(this.getClearState())
  }

  caseTwo() {
    this.setState({
      ...this.getClearState(), 
      qux: 'my QUX',
    })
  }

  render() {}
}
like image 24
Eduard Jacko Avatar answered Oct 03 '22 08:10

Eduard Jacko