简体   繁体   English

在 React js 中在页面之间导航和处理后退按钮的最佳方式

[英]Best way to navigate between pages and handle back button in react js

Hiii, My question is that, let assume that I have a signin page,The user can switch between the signin and signup pages, but what I want, is that when the user login or create a new account in the signup pages, all the history would be removed. Hiii,我的问题是,假设我有一个登录页面,用户可以在登录和注册页面之间切换,但我想要的是,当用户登录或在注册页面中创建新帐户时,所有的历史将被删除。 Or if it's not possible, what is the best way to handle those situations, to avoid that the user had login then when he clicks in the back button, he will find himself again in one of the two pages(signin or signout).或者,如果不可能,处理这些情况的最佳方法是什么,以避免用户登录,然后当他单击后退按钮时,他会再次发现自己位于两个页面之一(登录或注销)。 I am currently using React Router v4.我目前正在使用 React Router v4。 And it will be cool if anyone could help me and also give me the best practices for this.如果有人可以帮助我并为此提供最佳实践,那将会很酷。 thank you.谢谢你。

<BrowserRouter>
        <Switch>
          <Route path="/" component={SignIn} exact />
          <Route path="/signup" component={SignUp} />
          <Route path="/main" component={ContainerSingleApp} />
          <Route component={Error} />
        </Switch>
</BrowserRouter>

Have you read the react-router-v4 documentation that explains redirects with special emphasis on auth?您是否阅读过解释重定向并特别强调身份验证的 react-router-v4 文档 That is the method I have used in the past and it's worked really well.这是我过去使用过的方法,效果非常好。

The gist is that you create a <PrivateRoute /> component that wraps react-router's <Route /> component and basically will display the Login component if the user tries to navigate to a private route without being logged in. Otherwise the user will be given the view for the route that they requested.要点是您创建了一个<PrivateRoute />组件,该组件包装了 react-router 的<Route />组件,并且如果用户在未登录的情况下尝试导航到私有路由,则基本上将显示Login组件。否则,用户将被给予他们请求的路线的视图。

For example:例如:

<BrowserRouter>
    <Switch>
        <Route path="/" component={LandingPage} />
        <PrivateRoute path="/main" component={ContainerSingleApp} />
        <Route component={Error} />
    </Switch>
</BrowserRouter>

And from the documentation:从文档中:

function PrivateRoute({ component: Component, ...rest }) {
  return (
    <Route
      {...rest}
      render={props =>
        fakeAuth.isAuthenticated ? (
          <Component {...props} />
        ) : (
          <Redirect
            to={{
              pathname: "/login",
              state: { from: props.location }
            }}
          />
        )
      }
    />
  );
}

To solve the problem of having the user press the back button you can swap out the redirect in the PrivateRoute component with your Login component.要解决让用户按下后退按钮的问题,您可以将PrivateRoute组件中的重定向替换为Login组件。 That way you aren't actually doing a redirect to a route that points at your Login, you are simply rendering the Login in place of the private component that the user is trying to access.这样,您实际上并没有重定向到指向您的登录名的路由,您只是渲染登录名来代替用户尝试访问的私有组件。 Once the user is logged in, it will render the component that they are trying to see.用户登录后,它将呈现他们试图查看的组件。 Since there is no /login route needed for this approach, you don't need to worry about the user browsing back to the login page unless they are actually not logged in. See the changes in the code below:由于此方法不需要/login路由,因此您无需担心用户浏览回登录页面,除非他们实际上未登录。请参阅以下代码中的更改:

function PrivateRoute({ component: Component, ...rest }) {
  return (
    <Route
      {...rest}
      render={props =>
        fakeAuth.isAuthenticated ? (
          <Component {...props} />
        ) : (
          <Login />
        )
      }
    />
  );
}

The routes in the first coding example above demonstrate a setup where the landing page is not protected by authentication.上面第一个编码示例中的路由演示了一种设置,其中登录页面不受身份验证保护。

If you want your landing page to be a view that is protected with authentication (like your /main route that points at the ContainerSingleApp component) then you still wouldn't need to add the Login component as a route.如果您希望登录页面是受身份验证保护的视图(例如指向ContainerSingleApp组件的/main路由),那么您仍然不需要将Login组件添加为路由。 You could do something like this:你可以这样做:

<BrowserRouter>
    <Switch>
        <PrivateRoute path="/" component={ContainerSingleApp} />
        <Route component={Error} />
    </Switch>
</BrowserRouter>

Remember that in the PrivateRoute component it will display the login view automatically if the user isn't logged in. If they are, it will render the passed-in component.请记住,在PrivateRoute组件中,如果用户未登录,它将自动显示登录视图。如果已登录,它将呈现传入的组件。

It's important to note that the url will never show something like https://your-site.com/login .重要的是要注意 url 永远不会显示类似https://your-site.com/login It will always show the route for the component the user is attempting to access.它将始终显示用户尝试访问的组件的路由。 The difference being that the component that it renders will depend on whether the user is authenticated... The Login component if not, and the ContainerSingleApp component (or whatever component that route points to) if logged in.不同之处在于它呈现的组件将取决于用户是否经过身份验证......如果没有,则为Login组件,如果已Login则为ContainerSingleApp组件(或路由指向的任何组件)。

If you still want to create a route to the login page you can use a redirect:如果您仍然想创建一个到登录页面的路由,您可以使用重定向:

<BrowserRouter>
    <Switch>
        <PrivateRoute path="/" component={ContainerSingleApp} />
        <Route path="/signin" component={() => <Redirect to="/"/>}
        <Route path="/signup" component={Signup} />
        <Route component={Error} />
    </Switch>
</BrowserRouter>

See the redirect documentation .请参阅重定向文档 As for the Signup component... Just add logic in it that will redirect the user to another page if already logged in. You can use a similar approach that you used with the PrivateRoute component.至于Signup组件...只需在其中添加逻辑,如果已经登录,则将用户重定向到另一个页面。您可以使用与PrivateRoute组件一起使用的类似方法。

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

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