简体   繁体   English

如果使用 history.push 访问特定路由,React-Router v5 不会重定向

[英]React-Router v5 not redirecting if accessing specific route using history.push

In my react app I have /lead-manager route, /login route and there is / route, latter which is the app.js itself that I don't want anyone to access, if someone just goes to the / route I wanted them to redirect to /lead-manager route.在我的反应应用程序中,我有/lead-manager路由、 /login路由和/路由,后者是我不希望任何人访问的app.js本身,如果有人只是去我想要的/路由重定向到/lead-manager路由。 So this is what I did according to the solution given here .所以这就是我根据这里给出的解决方案所做的。

My app.jsx part我的 app.jsx 部分

import {
  BrowserRouter as Switch,
  useHistory,
  Route,
  useLocation,
} from "react-router-dom";

const App = ({ confirmAuthentication }) => {
  let location = useLocation();
  let history = useHistory();

  useEffect(() => {
    async function fetchIt() {
      if (localStorage.jwt !== undefined) {
        confirmAuthentication();
      }
    }
    fetchIt();
    if (location.pathname === "/") {
      history.push("lead-manager");
    }
  }, []);
 return (
    <div>
      <Switch>
        <Route exact path="/testing">
          <TestingComponent />
        </Route>
        <PrivateRoute path="/lead-manager" component={LeadApp} />
        <Route exact path="/login">
          <LoginView />
        </Route>
      </Switch>
    </div>
  );
}

The output I'm getting when I'm accessing / path is /lead-manager in the URL.我在访问/路径时得到的 output 是 URL 中的/lead-manager Which is the desired one, but the component doesn't render accordingly with components defined in /lead-manager route.这是所需的,但该组件不会与/lead-manager路由中定义的组件相应地呈现。 It just stays on / route even though URL shows /lead-manager即使 URL 显示/ /lead-manager

Hope this is clear希望这很清楚

In order to use history correctly you need to have a Router wrapping App component up in the hierarchy.为了正确使用历史记录,您需要在层次结构中使用路由器包装 App 组件。 Since you are rendering Router within App component, it isn't able to listen to the history changes since the history provided by useHistory doesn't refer to the history used by Router unless App component is wrapped by Router.由于您在 App 组件中渲染路由器,因此它无法监听历史更改,因为 useHistory 提供的历史不引用路由器使用的历史,除非 App 组件由路由器包装。

Do not render Router inside of App component不要在 App 组件内渲染路由器

Also you have rendered Router as the Switch component inside App component, you must instead import the exact Switch component from react-router-dom此外,您已将 Router 渲染为 App 组件内的 Switch 组件,您必须改为从react-router-dom导入确切的 Switch 组件

Check updated code below检查下面的更新代码

import {


    BrowserRouter as Router,
      Switch
      useHistory,
      Route,
      useLocation,
    } from "react-router-dom";

    const App = ({ confirmAuthentication }) => {
      let location = useLocation();
      let history = useHistory();

      useEffect(() => {
        async function fetchIt() {
          if (localStorage.jwt !== undefined) {
            confirmAuthentication();
          }
        }
        fetchIt();
        if (location.pathname === "/") {
          history.push("lead-manager");
        }
      }, []);
     return (
        <div>
          <Switch>
            <Route exact path="/testing">
              <TestingComponent />
            </Route>
            <PrivateRoute path="/lead-manager" component={LeadApp} />
            <Route exact path="/login">
              <LoginView />
            </Route>
          </Switch>
        </div>
      );
    }

    export default (props) => <Router><App {...props}/></Router>  

暂无
暂无

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

相关问题 在组件外部的 react-router v5 中以编程方式访问历史记录(history.push 等) - Programmatic access to history (history.push etc) in react-router v5 outside of components 如何在react-router v5中使用history.push传递对象? - How to pass object with history.push in react-router v5? history.push() 和自定义 URL 参数使用 react-router - history.push() and Custom URL parameters using react-router React-router您不能推同一条路线…强制History.push / reload组件 - React-router You cannot push the same route… force History.push / reload component 如何使用react react-router v4在axios拦截器中放置history.push位置? - how to do history.push location inside axios interceptor with react react-router v4? 防止 react-router history.push 重新加载当前路由 - Prevent react-router history.push from reloading current route 在这种情况下如何使用“history.push”? 反应路由器dom V5 - How Can I Use "history.push" In This Situation? react-router-dom V5 react-router-dom v5 - 链接有效,但 Redirect 和 history.push() 无效 - react-router-dom v5 - Link works, but Redirect and history.push() do not 如何识别路线是通过history.push还是手动更改地址发起的(react-router) - How to recognize if the route was initiated through a history.push or a manual adress change (react-router) React Router v5:history.push()改变地址栏,但不改变页面 - React Router v5: history.push() changes the address bar, but does not change the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM