[英]react setState array of object property failed with map es6
嘗試更新對象數組的狀態,但不知何故不能反映該狀態。
set_is_core = (selected_id) => {
const { main } = this.state
const next_main = main.map(o => {
if(o.main_id === selected_id){
console('trigger') //true
return ({
...o,
is_core: true
})
}
return o
})
this.setState({
main: next_main
}, ()=> console.log(this.state.main.some(o => o.is_core))//false?)
}
無法找出問題所在。
您正在嘗試映射對象。 使用es6只能映射陣列。 您可以將main
變量結構更改為數組,也可以使用lodash映射,這將允許您映射到對象https://lodash.com/docs#map
例如:
import {map} from 'lodash'
...
set_is_core = (selected_id) => {
const { main } = this.state
const next_main = map(main, (o) => {
if(o.main_id === selected_id){
console('trigger') //true
return ({
...o,
is_core: true
})
}
return o
})
this.setState({
main: next_main
}, ()=> console.log(this.state.main.some(o => o.is_core))//false?)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.