gpt4 book ai didi

reactjs - 使用 react-router-dom 5 防止有条件地返回

转载 作者:行者123 更新时间:2023-12-03 22:58:51 24 4
gpt4 key购买 nike

useEffect(() => {
return history.listen(_ => {
console.log(history.action);
if (history.action === 'POP' && (location.pathname === Settings_NewPersonPath || location.pathname === Settings_NewBusinessPath) && entityState.data) {
setEntityState({
...entityState,
submitted: false
});
}
})
}, [])
我正在模拟使用状态的多个页面。然而,当有人在第二个“页面”上,他们按下后退按钮时, 我只想更新状态而不是实际返回。
我该怎么做?

最佳答案

您可以使用 history.block 防止页面转换的 API。例子:

useEffect(() => {
const unblock = history.block(({ action, location, retry }) => {
// Conditional state manipulation
// Call unblock and retry to actually perform transition
});

return () => {
unblock();
};
}, []);

关于reactjs - 使用 react-router-dom 5 防止有条件地返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67812387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com