【发布时间】:2019-08-28 12:45:31
【问题描述】:
因此,当添加会话并提交表单时,我希望将客户的价格转移到我的会话状态中。这就是我在这部分代码中尝试做的事情。
state = {
id: null,
name: null,
duration: null,
dayOfWeek: null,
price: null
}
handleSubmit = (e) => {
e.preventDefault();
let newPrice = 0;
this.props.clientList.filter(client => {
if (client.name === this.state.name)
{newPrice = client.price}
this.setState({
price : newPrice
});
console.log("price = " + this.state.price, 'newPrice = ' + newPrice)
})
this.props.addSession(this.state);
e.target.reset();
this.setState({
id: null,
name: null,
duration: null,
dayOfWeek : null
});
}
发生了什么,我试图在控制台的图像中描绘,我添加的两个会话是,当我第一次添加它时,它会注销 price = null 和 newPrice = 40 ,第二次是 price = 40. 为什么我的代码在这里不起作用?
如果需要,我可以添加更多代码。让我知道你需要看什么,谢谢!
【问题讨论】:
-
我也刚刚意识到,在我将状态设置回 null 的代码的最后一部分中,我没有 price: null。当我添加它时,它甚至不能像以前那样第二次工作。
标签: javascript reactjs redux state