【问题标题】:React - homepage data is copied to other pagesReact - 将主页数据复制到其他页面
【发布时间】:2017-06-28 22:10:06
【问题描述】:

我正在使用带有 Auth0 身份验证的 React(来自 Auth0 页面的代码),我的问题是主页内容被复制到所有其他页面。我的 App 类中有一个导航栏,我想在所有页面上复制它(并且它被复制),但是添加到 App 类的任何其他内容也会被复制(我不想要)。我无法以这种方式找到有关使用道具和身份验证进行路由的适当文档。任何帮助将不胜感激。

Routes.js

const auth = new Auth();

const handleAuthentication = (nextState, replace) => {
  if (/access_token|id_token|error/.test(nextState.location.hash)) {
    auth.handleAuthentication();
  }
}

export const makeMainRoutes = () => {
  return (
    <BrowserRouter history={history} component={App}>
        <div>
          <Route path="/" render={(props) => <App auth={auth} {...props} />} />
          <Route path="/home" render={(props) => <Home auth={auth} {...props} />} />
          <Route path="/profile" render={(props) => (
            !auth.isAuthenticated() ? (
              <Redirect to="/home"/>
            ) : (
              <Profile auth={auth} {...props} />
            )
          )} />
          <Route path="/ping" render={(props) => (
            !auth.isAuthenticated() ? (
              <Redirect to="/home"/>
            ) : (
              <Ping auth={auth} {...props} />
            )
          )} />
          <Route path="/callback" render={(props) => {
            handleAuthentication(props);
            return <Callback {...props} /> 
          }}/>        
        </div>
      </BrowserRouter>
  );
}

App.js

class App extends Component {
  goTo(route) {
    this.props.history.replace(`/${route}`)
  }

  login() {
    this.props.auth.login();
  }

  logout() {
    this.props.auth.logout();
  }

  render() {
    const { isAuthenticated } = this.props.auth;

    return (
        <div>
            <nav>
                <div className="container nav-wrapper">
                    <a href="" onClick={this.goTo.bind(this, '')} className="brand-logo"><i className="material-icons">done_all</i> Rate IT</a>
                    <ul id="nav-mobile" className="right hide-on-med-and-down">
                        <li><a onClick={this.goTo.bind(this, 'home')}>Home</a></li>
                        {
                            !isAuthenticated() && (
                                <li><a onClick={this.login.bind(this)}>Log In</a></li>
                            )
                        }
                        {
                            isAuthenticated() && (
                                <li><a onClick={this.logout.bind(this)}>Log Out</a></li>
                            )
                        }
                    </ul>
                </div>
            </nav>
        </div>
    );
  }
}

export default App;

【问题讨论】:

    标签: reactjs routing auth0


    【解决方案1】:

    尝试将exact 属性传递给您的/home Route 组件,如下所示:

    &lt;Route exact path="/home" render={(props) =&gt; &lt;Home auth={auth} {...props} /&gt;} /&gt;

    这是 React Router v4 文档中的相关条目: https://reacttraining.com/react-router/web/api/Route/exact-bool

    【讨论】:

    • 这不起作用,但将exact 添加到&lt;Route path="/" ... 似乎是一个很好的步骤。 App.js 中的内容突然没有像我想要的那样复制到所有页面。这也意味着其他页面中没有导航栏。将&lt;nav&gt; 栏移动到单独的类并将其添加到其他页面,但是会抛出:Uncaught TypeError: Cannot read property 'replace' of undefined。所以我又卡住了。
    • 如果您的 App 组件只包含导航栏,则不需要将其包装在 Route 组件中 - 尝试将您的路线替换为:&lt;App auth={auth} {...props} history={history}/&gt; 如果这不起作用,尝试简单地import - 从它定义的文件中输入你的history
    • 另外,我不认为 BrowserRouter 组件接受 component 属性 - 删除它也不应该造成任何伤害。
    • &lt;App auth={auth} history={history}/&gt; 没有{...props} 创造了奇迹!道具未在 Route 组件之外定义。谢谢。
    猜你喜欢
    • 2018-07-10
    • 2015-11-04
    • 2021-09-13
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    相关资源
    最近更新 更多