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.

use array.prototype.slice:

pop: function() {   this.setstate({     mystack: this.state.mystack.slice(0, -1)   }); } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -