gpt4 book ai didi

javascript - 在 api 请求解析之前,componentWillMount 检查未定义

转载 作者:行者123 更新时间:2023-12-01 02:03:47 26 4
gpt4 key购买 nike

我有一个组件可以触发一些依赖于异步 API 请求的方法。我使用 componentWillmount 来检查一些 Prop 。如果此属性为 true,我会触发某个函数,否则为 false。但问题是,第一次 prop 是未定义,只有一段时间后它才会变成falsetrue。如何检查并等待请求解决?

componentWillMount = () => {
this.props.init(parseInt(this.props.tagId, 16), this.props.accessToken);
if (!this.props.notFound && !this.props.person) this.props.onFreeTag();
};

最佳答案

使用componentWillReceiveProps生命周期函数,例如:

componentWillReceiveProps = (nextProps) => {
if (!nextProps.notFound && !nextProps.person)
nextProps.onFreeTag()
}
}

关于javascript - 在 api 请求解析之前,componentWillMount 检查未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50288038/

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