[英]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
在层次结构中更高,并且Header
和navLeft
组件是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.