A problem of react-router

encapsulate the router as a component to write a login status locally when the user logs in. If true returns a route normally, if it is false, jump to the login page. In order to prevent users from jumping directly to the inner page without logging in

< hr >

requirement description is clear

step 1. Component encapsulation

const AuthRoute = (props) => {
    const isLogin = sessionStorage.getItem("isLogin") === "true";
    if(isLogin){
      return <Route {...props} />
    }else{
      return <Redirect to={
        {
          pathname: "/login",
          state: {
            from: props.location,
          }
        }
      } />
    } 
  };

step 2. Component call

 <AuthRoute exact path="/" component={Home} />
 <AuthRoute path="/System"component={System} />

question:
will report an error
Warning: You tried to redirect to the same route you"re currently on: "/ login"

if the router is BrowserRouter, how can I change this component to remove this warning without Switch?

Mar.14,2021

translate the error message, and you'll probably know the solution. If the current route is the same as the route you are about to jump, you should intercept or refresh it, instead of continuing to jump, the way to get the address of the route you are about to jump to is in props, and you can console it several times.

Menu