React修改两种种类型的state值:list、object
修改list类型的state
方式一:
通过list的concat方法,如下
let newList = this.state.selectedApiDetailList.concat(response.data) this.setState({selectedApiDetailList: newList})
方式二:
新增一个数组,将原来的数组与新的值加入数组
let newList = [...this.state.selectedApiDetailList, response.data] this.setState({selectedApiDetailList: newList})
修改object类型的state
方式一、Object.assign
let newHeaders = Object.assign({}, this.state.headers, {accessId: res.accessId}); this.setState({headers: newHeaders})
将List赋值给另外一个List
let a =["新增员工模块", "已有bug", "提交课程", "新增用户"] let b =[...a]
将Object赋值给另外一个Object
let tag = Object.assign({},this.state.tag)