React.js状态值为数组,怎么使用setState比较合适

React.js状态值为数组,怎么使用setState比较合适


鸿蒙传说
浏览 690回答 3
3回答

当年话下

React.js状态值为数组,那么使用setState设置状态的方法是调用onchang方法来操作数组。写法如下:onChange(event){var newArray = this.state.arr.slice();newArray.push("new value");this.setState({arr:newArray})}上面的方法中使用onChange来完成状态的动态改变。其中newArray 是把状态值数组进行拆分,然后调用push方法放入新值,再调用setState方法对数组进行更新。

慕神8447489

理论上来说,最好是setState({ 'arrary': [...this.state.array, newItem]})setState({ 'array' : this.state.array.concat([neItem])})
打开App,查看更多内容
随时随地看视频慕课网APP