Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove an attribute from a React component's state object

Tags:

reactjs

state

If I have a React component that had a property set on its state:

onClick() {     this.setState({ foo: 'bar' }); } 

Is it possible to remove "foo" here from Object.keys(this.state)?

The replaceState method looks like the obvious method to try but it's since been deprecated.

like image 878
Joshua Avatar asked Oct 01 '15 10:10

Joshua


People also ask

How do you unmount an element in React?

Unmount a React Node React has a top-level API called unmountComponentAtNode() that removes a component from a specific container. The function unmountComponentAtNode() takes an argument as a container from which the specific component should be removed.

How remove value from props in React?

No, you can't. The props of a react component are immutable and are not supposed to be changed by the component. If you need to work with the data locally, you could use the state of the component, or better create a local copy of the prop data.

Can you modify States directly React?

Prop Changes. It's important to note the difference between changes in state and changes in props. Changes in state and/or props will both trigger a re-render of our React component. However, changes in state can only happen internally due to components changing their own state.


2 Answers

You can set foo to undefined, like so

var Hello = React.createClass({     getInitialState: function () {         return {             foo: 10,             bar: 10         }     },      handleClick: function () {         this.setState({ foo: undefined });     },      render: function() {         return (             <div>                 <div onClick={ this.handleClick.bind(this) }>Remove foo</div>                 <div>Foo { this.state.foo }</div>                 <div>Bar { this.state.bar }</div>             </div>         );     } }); 

Example

Update

The previous solution just remove value from foo and key skill exists in state, if you need completely remove key from state, one of possible solution can be setState with one parent key, like so

var Hello = React.createClass({    getInitialState: function () {      return {        data: {          foo: 10,          bar: 10        }      }    },      	    handleClick: function () {      const state = {        data: _.omit(this.state.data, 'foo')      };            this.setState(state, () => {        console.log(this.state);      });    },              render: function() {      return (        <div>          <div onClick={ this.handleClick }>Remove foo</div>          <div>Foo { this.state.data.foo }</div>          <div>Bar { this.state.data.bar }</div>        </div>      );    }  });    ReactDOM.render(<Hello />, document.getElementById('container'))
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>  <div id="container"></div>
like image 168
Oleksandr T. Avatar answered Oct 01 '22 05:10

Oleksandr T.


var Hello = React.createClass({ getInitialState: function () {     return {         foo: 10,         bar: 10     } },  handleClick: function () {     let state = {...this.state};     delete state.foo;     this.setState(state); },  render: function() {     return (         <div>             <div onClick={ this.handleClick.bind(this) }>Remove foo</div>             <div>Foo { this.state.foo }</div>             <div>Bar { this.state.bar }</div>         </div>     ); } 

});

like image 33
GAURAV Avatar answered Oct 01 '22 06:10

GAURAV