操作系统:Windows 10 Pro
反应路由器:4.1.1
反应:15.5.4
因此,我在 a 中进行条件检查<Route />
以确定 authToken 是否存在,如果它不重定向用户登录,否则呈现关联的组件。但即使令牌存在,如图所示,如果您重新加载 root/
或/view/:postId
.
我在这里忽略了什么?
应用程序.js
render () {
const SOME_PATH = window.location.pathname;
return (
<ApolloProvider store={store} client={client}>
<Router history={history}>
<Route path={SOME_PATH} component={App}/>
</Router>
</ApolloProvider>
)
}
应用程序.js
export default compose(
allPostsCommentsQuery,
connect(mapStateToProps, mapDispatchToProps)
)(Main);
主.js
render () {
const auth = this.props.auth.token;
console.log('auth = ', auth);
return (
<div>
<h1>
<Link to="/">Flamingo City</Link>
</h1>
<Switch>
<Route path={`${this.props.match.url}`} exact render={(props) => (
!auth ? <Redirect to="/login"/> : <PhotoGrid {...this.props} {...props} />
)} />
<Route path={`${this.props.match.url}view/:postId`} render={(props) => (
!auth ? <Redirect to="/login"/> : <Single {...this.props} {...props} />
)} />
<Route path={`${this.props.match.url}login`} render={(props) => (
<LoginUser {...this.props} {...props} />
)} />
</Switch>
</div>
);
}