【问题标题】:Add component to every reactJS component将组件添加到每个 reactJS 组件
【发布时间】:2014-08-26 06:01:49
【问题描述】:

我正在尝试在所有页面的顶部放置一个导航栏。这是路由器:

App = React.createClass({
    render: function() {
        <div>
            <NavBar />
            <Locations hash className="Router">
                <Location path="/" handler={MainPage} />
                <Location path="/help" handler={HelpPage} />
                <Location path="/about" handler={AboutPage} />
                <NotFound handler={NotFoundPage} />
            </Locations>
        </div>
    }
});

注意Locations 标记中的哈希参数。当我这样使用路由器时,&lt;NavBar /&gt; 组件中的链接不使用哈希。但是,如果我在每个单独的组件中包含&lt;NavBar /&gt;,那么哈希链接将按预期工作。是否有一个地方可以包含&lt;NavBar /&gt;,以便在路由器显示的所有页面上呈现它?

【问题讨论】:

    标签: javascript reactjs react-jsx


    【解决方案1】:

    通过将路由器从 react-router-component 切换到 react-router 解决。

    var App = React.createClass({
      render: function() {
        return (
          <div>
            <NavBar />
            {this.props.activeRouteHandler}
          </div>
        );
      }
    });
    
    React.renderComponent((
      <Route handler={App}>
        <Route name="main" path="/" handler={MainPage}/>
        <Route name="help" handler={HelpPage}/>
        <Route name="about" handler={AboutPage}/>
        <Route name="notfound" path="*" handler={NotFoundPage}/>
      </Route>
    ), document.body);
    

    【讨论】:

    • 请包含您当前的代码,以便帮助其他找到此问题的人。
    • 请注意 API 已更改为 this.props.activeRouteHandler() :)
    猜你喜欢
    • 2017-12-02
    • 2019-05-13
    • 1970-01-01
    • 2013-08-07
    • 2016-03-13
    • 2019-09-08
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多