【问题标题】:react router not getting route after click点击后反应路由器没有获得路由
【发布时间】:2016-01-28 04:36:46
【问题描述】:

我的routes.js 类似于:

const routes =  (
  <Route path="/" component={App}>
        <Route path="/login" component={Login} />
        <Route path="/one" component={One} />
        <Route path="/two" component={Two} />
  </Route>
)
export default routes

我在App 组件中有一个头文件,我的头文件看起来像:

class Header extends Component {
  render() {
    return (
      <div>
        <h3><Link to={`/one`}>One</Link></h3>
        <h3><Link to={`/two`}>Two</Link></h3>
      </div>
    )
  }
}

export default Header

当我运行localhost:3333 时,它会转到包含HeaderOneTwoApp 页面。但是当我点击它时,它没有显示OneTwo 的内容或组件。

我在路由器中缺少什么?

需要帮助。

【问题讨论】:

  • 你的this.props.children在哪里?

标签: reactjs react-router react-routing


【解决方案1】:

我猜你错过了this.props.children。所以你的组件应该看起来像

class Header extends Component {
  render() {
    return (
      <div>
        <h3><Link to={`/one`}>One</Link></h3>
        <h3><Link to={`/two`}>Two</Link></h3>
        <div>{this.props.children}</div> // this is the way how React renders different components based on route 
      </div>
    )
  }
}

export default Header

React-Route 链接了解更多信息。 我希望它会帮助你。

谢谢

【讨论】:

    猜你喜欢
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2022-01-14
    • 2018-06-02
    • 2021-10-29
    相关资源
    最近更新 更多