简体   繁体   English

在 react-router-dom v6 中管理历史记录

[英]Manage history in react-router-dom v6

How do I manage history in my react project using react-router-dom v6 ?如何使用react-router-dom v6管理我的反应项目中的历史记录? Currently what is happening is that when the user clicks on the browsers' back button it takes him to the default browsers' page instead of the previous page he visited on the website.目前发生的情况是,当用户点击浏览器的后退按钮时,它会将他带到默认浏览器的页面,而不是他在网站上访问的上一个页面。

Here's the code这是代码

index.tsx索引.tsx

ReactDOM.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>,
  document.getElementById("root")
);

App.tsx应用程序.tsx

<ErrorBoundary>
   <Router />
</ErrorBoundary>

Router.tsx路由器.tsx

const Router: React.FC = (): JSX.Element => {
  return (
    <Suspense fallback={<Loader loadingText="Loading..." />}>
      <Routes>
        <Route path="" element={<RouteGuard />}>
          <Route path="" element={<Home />} />
        </Route>
        <Route path="*" element={<Error404 />} />
      </Routes>
    </Suspense>
  );
};

Found the solution, In the navigate { replace: true } was used and as per the documentation找到了解决方案,在导航{ replace: true }中使用并按照文档

replace: true, the navigation will replace the current entry in the history stack instead of adding a new one. replace: true,导航将替换历史堆栈中的当前条目,而不是添加新条目。

Hence removing it worked well for me, sorry for the troubles caused and thanks for trying to help因此删除它对我来说效果很好,对造成的麻烦感到抱歉,并感谢您尝试提供帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM