React.js: setState overwriting, not merging

后端 未结 3 1190
抹茶落季
抹茶落季 2020-12-04 14:13

I\'m quite new to React.JS and I am in the process of experimenting by building a masonry-style layout.

I render each element to the DOM, then I need to loop over eac

3条回答
  •  执笔经年
    2020-12-04 15:00

    Something like:

    getInitialState: function() {
        return {
            something: { x: 0, y: 0 },
            blah: 10
        };
    }
    
    var state = Object.assign(this.state, {
        something: Object.assign(this.state.something, { y: 50 }),
    });
    
    this.setState(state);
    

    Would be better if it was recursive/deep rather than hard coding the tree, but I will leave that up to the reader :)

提交回复
热议问题