0

操作系统: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>
    );
  }

在此处输入图像描述

4

1 回答 1

1

重构代码如下解决了这个问题:

!auth ? (<Redirect to="/login"/>) : (<PhotoGrid {...this.props} {...props} />)
于 2017-06-13T21:07:48.047 回答