简体   繁体   English

当用户未登录 React 时隐藏 app.js 中的组件

[英]hide components in app.js when a user is not logged in React

I am creating an application in react, I have a login page, and other pages that need the user to be logged in to access.我正在创建一个反应应用程序,我有一个登录页面,以及其他需要用户登录才能访问的页面。

in my app.js在我的 app.js

I have a whole structure that I created with components.我有一个用组件创建的完整结构。

  render() {

    return (
      <BrowserRouter>

        <Header showNavLeft={this.state.showNavLeft} onClickBars={(value) => this.setState({ showNavLeft: value })} />
        <Container>
          <NavLeft showNavLeft={this.state.showNavLeft} />
          <Screen>
            <Routes />
          </Screen>
        </Container>


        <GlobalStyle />
      </BrowserRouter>
    );
  }
}

And I have the following route file我有以下路线文件

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

const Routes = () => (

    <Switch>
        <Route path="/" exact component={Login} />
        <PrivateRoute path="/home" component={home} />
        <PrivateRoute path="/users" component={users} 
    </Switch>
)

When I access the login page, I don't want all of my Header, NavLeft, Container components to be loaded.当我访问登录页面时,我不想加载我的所有 Header、NavLeft、Container 组件。

What is the best way to validate this?验证这一点的最佳方法是什么?

I'm stuck in this situation, I can't think of how to get around this我被困在这种情况下,我想不出如何解决这个问题

Flip it around so that Routes is higher up the hierarchy, and the Header and navLeft components are a separate part of a Wrapper component.翻转它,使Routes在层次结构中更高,并且HeadernavLeft组件是Wrapper组件的独立部分。

const Wrapper = ({children} => (
<>
  <Header showNavLeft={this.state.showNavLeft} onClickBars={(value) => this.setState({ showNavLeft: value })} />
      <Container>
        <NavLeft showNavLeft={this.state.showNavLeft} />
          <Screen>
            {children}      
          </Screen>
      </Container>
<GlobalStyle />
</>

Then you want to put the Wrapper as the component prop on your Route, and then the home compnent should be a child of it, like:然后你想把 Wrapper 作为组件道具放在你的 Route 上,然后 home 组件应该是它的子组件,例如:

<Switch>
<Route exact path="/">
  <Login/>
</Route>
<Route path="/home" component={Wrapper}>
  <home/>
</Route>
<Route path="/user" component={Wrapper}>
  <Users />
</Route>
</Switch>

Or:或者:

<Switch>
<Route exact path="/">
  <Login/>
</Route>
<Route path="/home" component={Wrapper}>
  <Wrapper>
    <home/>
  </Wrapper>
</Route>
<Route path="/user">
  <Wrapper>
    <Users />
  </Wrapper>
</Route>
</Switch>

It might also be easier if you refactor to get rid of the separate PrivateRouter, but have everything in one place, then re-jig things so that the header and navbar are how you like, and then you can see what you want to split out later.如果您重构以摆脱单独的 PrivateRouter 也可能更容易,但将所有内容放在一个地方,然后重新调整事物以便 header 和导航栏是您喜欢的方式,然后您可以看到要拆分的内容之后。

So The easiest way is by Taking the Header, Container & NavLeft components from where it is now and moving it to inside PrivateRoute wrapping the component returned in isAuthenticated.因此,最简单的方法是从现在的位置获取 Header、Container 和 NavLeft 组件并将其移动到 PrivateRoute 内部,包装在 isAuthenticated 中返回的组件。

So that it shows up only in Private routes.所以它只显示在私有路由中。

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

相关问题 外部 React 组件未在 App.js 中呈现 - External React components are not rendering in App.js React 功能组件未在 App.js 中呈现 - React functional components not rendering in App.js React App.js 在进行更改时不会更新,但其他组件会更新 - React App.js doesn't update when making change but other components do 如何在 React app.js bundle 中隔离组件 - How to isolate components in React app.js bundle React 组件和 App.js 错误:元素类型无效 - React Components & App.js Error: Element Type is Invalid React Router Dom 组件在 App.js 中呈现,但不是作为单独的组件 - React Router Dom components rendering in App.js, but not as separate component 不在 App.js 中时反应 leaflet map 位置故障 - React leaflet map location glitches when not in App.js 我可以检查 Auth0 用户是否已经在我的自定义 NextJS _app.js 级别登录? - Can I check if a Auth0 user is logged in already at the level of my custom NextJS _app.js? 在 React 中使用登录和注销的用户渲染组件 - Render components with logged in and logged out user in React 如何在 Header、Footer 等组件中使用 React Router 而不是 App.js - How to use React Router in Header, Footer and etc components instead of App.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM