需求:从一个页面跳转到另一个页面,并且打开跳转后页面内的一个弹窗
思路:用props.history.push()实现跳转,并且传递state到第二个界面,第二个界面接收到state后,改变弹窗状态。
代码如下:
//first-page
props.history.push({
pathname: '/second-page',
state: {
visible: true
}
})
//second-page
const fromFirstPage = () =>{
if(props.location.state && props.location.state.visible){
_setVisible(true)
let state = {...props.location.state}
delete state.visible
props.history.replace({...props.location, state}) // 清空路由参数
}
}