0
我想將新元素添加到我的產品狀態,目前使用.concat
,但它添加到數組的末尾,但我需要添加到開頭。反應如何將新元素添加到狀態?
var Body = React.createClass({
getInitialState() {
return { products: [] }
},
handleSubmit(product) {
var newState = this.state.products.concat(product);
this.setState({ products: newState });
},
});
試過用this.state.products.unshift(product)
但它給我Uncaught TypeError: this.state.tickets.unshift is not a function
這可能是它返回一個數組的長度不是本身。