javascript - Proper way to pop from a react component's array-type state attribute? -
let's have react component like:
var mycomponent = react.createclass({ getinitialstate: function() { return { mystack: [] }; }, ... pop: function(a) { // concise , elegant way pop array type state? } }
maybe write
pop: function() { var clone = _.clone(this.state.mystack); clone.pop(); this.setstate({mystack: clone}); }
but looks ugly... know works looking @ code becomes annoying when write these codes.
is there nice way popping array type react component state?
i implemented push()
like
push: function(a) { this.setstate({mystack: this.state.mystack.concat([a])}); }
in single line.
i believe there nice 1 line solution pop
, too.
pop: function() { this.setstate({ mystack: this.state.mystack.slice(0, -1) }); }
Comments
Post a Comment