gpt4 book ai didi

javascript - this.setState 在 componentDidUpdate 中不起作用

转载 作者:行者123 更新时间:2023-12-01 00:21:08 26 4
gpt4 key购买 nike

这段代码存在一些问题,我认为是因为``setState 在此代码中不起作用。

this.setState({ 
showTooltip: true,
toolTipsValue: message,
error: true
})

点击按钮之前

this.props.loginRes = {
responseCode: 1,
result: {}
}

点击按钮后

prevProps.loginRes = {
responseCode: 1,
result: {}
}
this.props.loginRes = {
responseCode: 1,
result: {
data: {},
statusCode: 1,
statusMessage: 'Invalid email/mobile'
}
}
componentDidUpdate(prevProps, prevState) {
if (this.props.loginRes !== prevProps.loginRes) {
const message = this.props.loginRes.result.statusMessage;
this.setState({
showTooltip: true,
toolTipsValue: message,
error: true
})
}
}

错误信息 Error Message Link

最佳答案

作为 winwiz1 的补充。 (他说的是正确的)。

您正在 componentDidUpdate 函数中比较两个对象。用 !== 比较两个对象是行不通的。为什么它不起作用的答案可以在这里找到:Comparing two objects

解决方案是 lodash 和 isEqual,示例可以在这里找到: Depp comparison between 2 objects

关于javascript - this.setState 在 componentDidUpdate 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59422762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com