【发布时间】:2021-05-06 21:57:22
【问题描述】:
如果我有一个功能可以在您单击后退按钮时创建确认弹出窗口,我想在导航回搜索页面之前保存状态。顺序有点奇怪,有一个搜索页面,然后是提交表单页面和摘要页面。我在到达路由器中将替换设置为 true,因此当我返回摘要页面时,它会转到搜索页面。我想保留历史并将提交数据的状态传递到历史中,所以当我单击前进时,它会返回页面而不会出错。
我查阅了一堆指南并浏览了一些文档,我想我对如何构建这个有了一个很好的想法,但是在这个组件中,我们正在解构道具,所以我该如何通过那些进入历史的状态变量?
export const BaseSummary = ({successState, children}: BaseSummaryProps) => {
let ref = createRef();
const [pdf, setPdf] = useState<any>();
const [finishStatus, setfinishStatus] = useState(false);
const onBackButtonEvent = (e) => {
e.preventDefault();
if (!finishStatus) {
if (window.confirm("Your claim has been submitted, would you like to exit before getting additional claim information?")) {
setfinishStatus(true);
props.history.push(ASSOCIATE_POLICY_SEARCH_ROUTE); // HERE
} else {
window.history.pushState({state: {successState: successState}}, "", window.location.pathname);
setfinishStatus(false);
}
}
};
useEffect(() => {
window.history.pushState(null, "", window.location.pathname);
window.addEventListener('popstate', onBackButtonEvent);
return () => {
window.removeEventListener('popstate', onBackButtonEvent);
};
}, []);
另外我没有传入子变量,因为历史不会克隆 html 元素,我只想传入为该组件返回的表单数据以相应地呈现信息
【问题讨论】:
标签: reactjs typescript browser-history reach-router