react.js - 处理固定页眉和页脚的React-router

Kos*_*ika 10 javascript css reactjs ratchet-2 react-router

我有反应路由器调整的React.js应用程序,我对我当前的路由处理有疑问.

设计如下,常见的移动布局,固定的页眉和页脚,中间的内容:

在此输入图像描述

在它们是静态的情况下,我可以简单地创建这样的结构:

<RatchetHeader />
<RatchetFooter />
<RouteHandler />
Run Code Online (Sandbox Code Playgroud)

但偶尔他们会在页面之间进行更改,例如:

  • 标题和按钮文本
  • 按钮数量
  • 某些页面上没有页脚

将它们放在视图控制器中并在每次重新渲染时更好RouteHandler吗?

Ale*_*oro 14

我不知道Ratchet的细节,但从一般的反应来看,在你的情况下,将页脚放在RouteHandler中更好,这样你就可以根据自己的喜好定义它的存在.

对于标题,我相信你总是喜欢在那里?在这种情况下,您可以将其保留在Handler之外并传递它的属性,而不是更改它的布局.

最终结果看起来与此类似(组件导入是暗示的,因此我不是为了保持对逻辑的关注而不包括它们):

app.js:

<Route handler={AppHandler}>
  <DefaultRoute handler={HomeHandler}/>
  <Route name='foo' path='/foo' handler={FooHandler}/>
  <Route name='bar' path='/bar' handler={BarHandler}/>
  <NotFoundRoute handler={NotFoundHandler}/>
</Route>
Run Code Online (Sandbox Code Playgroud)

);

App.react.js:

<div>
  <Header title={this.state.title}/>
  <RouteHandler {...this.props}/>
</div>
Run Code Online (Sandbox Code Playgroud)

所述Header.react.js -使用用于说明一些虚分量:

var Header = React.createClass({
  render: function(){
    return (
      <div>
        <Button type="previous" title="Left"/>
        <HeaderTitle>{this.props.title}</HeaderTitle>
        <Button type="next" title="Right"/>
      </div>
    );
  }
});

module.exports = Header;
Run Code Online (Sandbox Code Playgroud)

Foo.react.js:

var Foo = React.createClass({
  render: function(){
    var footerActions = [ // Ideally you'd get this from a constants file or something alike.
      {
        'actionType': 'viewHome',
        'actionIcon': 'icon-home',
        'actionLabel': 'Home'
      },
      {
        'actionType': 'viewProfile',
        'actionIcon': 'icon-profile',
        'actionLabel': 'Profile'
      },
      {
        'actionType': 'viewFavorites',
        'actionIcon': 'icon-favorites',
        'actionLabel': 'Favorites'
      },
      ...
    ];
    return (
      <div>Your content here</div>
      <Footer actions={footerActions}/>
    );
  }
});

module.exports = Foo;
Run Code Online (Sandbox Code Playgroud)

Footer.react.js:

var Footer = React.createClass({
  render: function(){
    var actionItems = this.props.actions.map(function(item){
      return (<ActionItem action={item.actionType} icon={item.actionIcon} label={item.actionLabel}/>);
    });
    return (
      <div>{actionItems}</div>
    )
  }
});

module.exports = Footer;
Run Code Online (Sandbox Code Playgroud)

然后,在Bar.react.js你可能只是不包括<Footer>组件,如下所示:

Bar.react.js:

var Bar = React.createClass({
  render: function(){
    return (
      <div>Your content here</div>
    );
  }
});

module.exports = Bar;
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!