Javascript 渲染后未调用componentDidUpdate
我需要连接到组件更新事件,以获取新数据。因此,我写了这样的东西:Javascript 渲染后未调用componentDidUpdate,javascript,reactjs,react-router,Javascript,Reactjs,React Router,我需要连接到组件更新事件,以获取新数据。因此,我写了这样的东西: ... constructor(props) { super(props); const values = queryString.parse(this.props.location.search), type = values['type']; this.props.loadChallengeDetails(type); } shouldComponentUpdate() { console.log('
...
constructor(props) {
super(props);
const values = queryString.parse(this.props.location.search),
type = values['type'];
this.props.loadChallengeDetails(type);
}
shouldComponentUpdate() {
console.log('shouldComponentUpdate');
/* if (this.props.location.search !== nextProps.location.search) {
console.log('should reload report');
const values = queryString.parse(nextProps.location.search),
type = values['type'];
this.props.loadChallengeDetails(type);
} */
return true;
}
componentDidUpdate(prevProps) {
console.log('componentDidUpdate');
if (this.props.location.search !== prevProps.location.search) {
console.log('should reload report');
const values = queryString.parse(this.props.location.search),
type = values['type'];
this.props.loadChallengeDetails(type);
}
}
render() {
console.log('details:', this.props.details);
...
如果您愿意,此页面/视图是“母版”页面的“详细信息”。根据查询中传递的type
的值,它会获取一些数据
第一次渲染时效果很好。调用了shouldComponentUpdate
或componentdiddupdate
生命周期方法。然而,当我返回查看另一条记录的详细信息时,它会显示过时的数据。控制台日志在渲染内部打印详细信息
,但不调用shouldComponentUpdate
或componentDidUpdate
我做错了什么?请告知。什么是
this.props.loadChallengeDetails
do?从redux商店加载this.props.details
。您是否缺少组件的withRouter
包装器?@madebydavid为什么withRouter
甚至重要,如果render()
被调用?@IgorShmukler-此.props.location
仅当您使用带路由器的时才会设置