[英]In React, can I check where the user will go with history.goBack()
I'm using react-query and I want to send the user back to the previous page when they save a form that triggers a query invalidation-- except in the case in which going back will send the user to the current page (the user's history started on that page), as this causes an infinite loop.我正在使用 react-query 并且我想在用户保存触发查询失效的表单时将其发送回上一页 - 除非返回会将用户发送到当前页面(用户的历史开始于该页面),因为这会导致无限循环。
Here's my code:这是我的代码:
const history = useHistory();
const mutatePost = useMutation(
(jsonData : InterfaceJsonData) => fetchPostWithUserAuth(
postUrl(id), fetchOptionsPost(jsonData, id),
), {
onSuccess: () => {
history.goBack();
How can I check where history.goBack()
goes?如何检查
history.goBack()
的去向? In the rare case that the user started on the current page, I don't want to accidentally create an infinite loop.在用户从当前页面开始的极少数情况下,我不想意外创建无限循环。
I'm using history.goBack()
because the component is used in three places, and I want to redirect to the relevant page (one of three pages).我正在使用
history.goBack()
因为该组件在三个地方使用,并且我想重定向到相关页面(三个页面之一)。 If there is some way to search the history to check which of the three previous pages was visited, that would be perfect.如果有某种方法可以搜索历史记录以检查前三个页面中的哪一个被访问过,那将是完美的。
You can build a history stack when the user arrives at your site, you push into an array every single page, where the user navigated.当用户到达您的站点时,您可以构建一个历史堆栈,您可以将用户导航的每个页面都推送到一个数组中。 Then if the user pushes the back button, you know what's happening.
然后,如果用户按下后退按钮,您就会知道发生了什么。 If the array is empty, you know, this was the first page.
如果数组为空,你知道,这是第一页。 Or another solution might be this .
或者另一个解决方案可能是this 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.