简体   繁体   English

在 React 中,我可以使用 history.goBack() 检查用户将在哪里 go

[英]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.

相关问题 我如何 go 到盖茨比的上一页(history.goBack) - How do I go to previous page in gatsby (history.goBack) 使用 history.goBack() 检测用户是否到达当前路线 - Detect if the user got to the current route by using history.goBack() 等待react-router history.go返回完成 - Await for react-router history.goBack to complete 在 history.goback() 之后 React 组件没有加载道具 - React component is not loading props after history.goback() react-router-dom history.goBack() 不适用于 Firefox 和 Safari - react-router-dom history.goBack() not working on Firefox and Safari 使用 history.goBack() 发送数据 - sending data with history.goBack() react js是否history.push和history.goBack重新安装前一页/组件? - react js does history.push and history.goBack re mounts previous page/component? 使用history.goBack()时,如何保证app内只返回go个页面? - When using history.goBack(),how to ensure only go back to pages within app? React Router history.goBack 在使用时不起作用<redirect /> - React Router history.goBack doesn't work when using <Redirect /> Ionic/React/TypeScript,使用 react-router history.push,history.replace 和 history.goBack Animation 触发两次 - Ionic/React/TypeScript,using react-router history.push, history.replace and history.goBack Animation triggers twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM