当我渲染我的页面时,我正在使用componentWillReceiveProps(nextProps)
访问道具。在初始加载时,道具会进入页面,但是,如果我点击内部链接,然后再次点击页面,componentWillReceiveProps(nextProps)
似乎会被跳过,所以道具不会加载。从我的阅读看来,这似乎是正确的,我只是不知道解决方案是什么。任何帮助?如果页面未刷新,componentWillReceiveProps会跳过加载
componentWillReceiveProps(nextProps) {
const profileCandidateCollection = nextProps.profileCandidate;
const profileCandidateCollectionId = profileCandidateCollection._id;
const summary = profileCandidateCollection && profileCandidateCollection.summary;
console.log("summary: ", summary);
this.setState({
summary: summary || '',
profileCandidateCollectionId: profileCandidateCollectionId || null
});
}
叶,我只是想你的建议。当我把它放在构造函数中时,'data'对象返回空。 – bp123
在构造函数和'componentWillRecieveProps'方法中都使用,'constructor'将在初始渲染时被调用,并且componentWillRecieveProps'将在道具更新时被调用。 –
谢谢,这工作。虽然它看起来不太优雅。这是'最佳实践'吗? – bp123