我正在将excel电子表格转换为webapp。我有一个问题是如何更新作为嵌套子级的“ targetLevel”值。我如何使这种状态成为可能?例如:targetLevel = averagePolicy * appsPerWeek * productionWeeks
(状态中还存在其他分组的值,这就是为什么它是嵌套的)
this.state = {
personalValues: [
{targetLevel: 0},
{firstCommission: 90 },
{cancels: 0},
{averagePolicy: 0},
{productionWeeks: 0},
{presentationsPW: 0},
{closing: 0},
{appsPerWeek: 0}
]
我尝试过的函数未获取值:(onBlur)
changeValues(event){
this.setState(()=> {
const total = this.state.personalValues[3]['averagePolicy'] * this.state.personalValues[7]['appsPerWeek'] * this.state.personalValues[4]['productionWeeks'];
return {
personalValues: [
targetLevel: total
]
}
});
}
表单更改方法(onChange)
handleFormChange(event){
this.setState({[event.target.name]: event.target.value})
}
我提供了一个gif以便澄清
答案 0 :(得分:0)
也许您可以尝试以下类似方法来覆盖targetLevel的值
changeValues(event){
const total = this.state.personalValues[3]['averagePolicy'] * this.state.personalValues[7]['appsPerWeek'] * this.state.personalValues[4]['productionWeeks'];
this.setState(prevState => ({
personalValues: prevState.personalValues.map(
obj => Object.assign(obj, { targetLevel: total }));
}));
}
答案 1 :(得分:0)
我认为您的状态正在突变,新的状态分配似乎不正确。请尝试以下代码。
const total = this.state.personalValues[3]['averagePolicy'] * this.state.personalValues[7]['appsPerWeek'] * this.state.personalValues[4]['productionWeeks'];
const newStateValue = [...this.state.personalValues];
newStateValue[0] = {targetLevel: total};
return {
personalValues: newStateValue
}