react引入react-router-dom,通过props.history跳转页面,url发生了变化,但页面没有更新(常出现情况:主路径不变、查询字符串改变)。这种情况我们可以通过以下办法解决
如果是class组件,可以通过shouldComponmentUpdate将页面重新渲染
如果是class组件,可以通过componentWillReceiveProps将页面重新渲染
如果是函数式组件,可以通过useMemo模拟shouldComponmentUpdate,将页面重新渲染
创建一个中转页面,先跳到中转页面,再从中转页面跳到目标页面(不推荐)
示例:
componentWillReceiveProps(nextProps: any) { if (this.props.location.search!==nextProps.location.search) { console.log('通过判断nextProps和当前props的差异,决定页面是否更新') console.log('执行页面初始化操作') } }