【发布时间】:2019-07-10 09:59:13
【问题描述】:
const state = fromJS({
poolName: {
poolNameCost:
poolNamePercentage:
poolNameShare:
}
})
poolName 因操作而异。
如果其中任何一个更新,我必须更新所有属性 poolNameCost, poolNamePercentage, poolNameShare。
我愿意
const { poolName, val, fieldSuffix, initialCost, initialShare } = action.payload;
state.update(`${poolName}`,
items => ({
...items,
[`${poolName}Cost`]() {
if (suffix === 'Share') return (val / initialCost) * initialShare;
if (suffix === 'Percentage') return (val / initialCost) * 100;
return val
},
[`${poolName}Share`]() { //.....// },
[`${poolName}Percentage`]() { //.....// },
})
)
我得到一个空对象poolName = {}。为什么会这样?
上面的代码是下面代码的重构版本,效果很好。
代码,
(suffix === 'Cost') &&
state.update(`${poolName}`,
items => ({
...items,
[`${poolName}Share`]: (val / initialCost) * initialShare,
//...similarly for other sibling property of `Share` in `poolName`...//
})
)
值正确更新。
【问题讨论】: