reactjs - 如何替换 UNSAFE_componentWillReceiveProps
问题描述
我是反应 JS 的新手。如何将 UNSAFE_componentWillReceiveProps 替换为 componentDidUpdate ?下面是我的代码:
componentDidMount() {
this.updateDate();
}
UNSAFE_componentWillReceiveProps() {
this.updateDate();
}
updateDate() {
if (typeof window !== null) {
var base = Router.query.props[0];
var quote = Router.query.props[1];
this.props.GlobalStore.update_detail('pair_name', base + '/' + quote);
}
}
为什么我使用它,因为我需要在更改 URL 参数时重新渲染组件。我正在使用 Next JS
解决方案
从 React 推荐:替换 UNSAFE_componentWillReceiveProps() 你可以:
代替
UNSAFE_componentWillReceiveProps() {
this.updateDate();
}
经过
componentDidUpdate() {
this.updateDate();
}