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

后端 未结 10 1992
南笙
南笙 2020-12-06 09:17

If I have a react component that had a property set on it\'s state:

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

Is it possible to re

10条回答
  •  有刺的猬
    2020-12-06 09:42

    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 (
            
    Remove foo
    Foo { this.state.foo }
    Bar { this.state.bar }
    ); }

    });

提交回复
热议问题