[译]基于 React Router 4 的可复用 Layout 组件
在 React Router V4 应用中,希望在每个路由上呈现一些默认的组件,比如页眉和页脚: <div className="App"> <div className="Header"> Page Header </div> <div className="Content"> {content that changes} </div> <div className="Footer"> Page Footer </div> </div> 在最新版本的 React Router V4 中可以很容易实现这一点,通过为特定的用例创建子布局。 创建默认布局我们把每个页面都使用的布局作为默认布局。当页面路由和地址栏匹配的时候,React router 会调用 // 调用组件的常规方式 <Route path="/" component={SomeComponent} /> // 使用 render 属性可以最组件做一些自定义操作 <Route path="/" render={matchProps => <SomeComponent {...matchProps} />} /> 这是非常有用的,因为我们可以把这些组件放到 const DefaultLayout = ({component: Component,...rest}) => { return ( <Route {...rest} render={matchProps => ( <div className="DefaultLayout"> <div className="Header">Header</div> <Component {...matchProps} /> <div className="Footer">Footer</div> </div> )} /> ) }; <DefaultLayout path="/" component={SomeComponent} />
通过向 一个非常重要的注意点是把 <component /> // 转换为 React.createElement("component",null); // 不是我们需要的 <Component /> // 转换为 React.createElement(Component,null); // 现在,这是一个 React 组件 阅读 facebook 官方文档 "用户自定义组件必须是大写字母开头" 获取更多信息。 扩展默认布局我们的默认布局已经包含了在每个页面上的需要共享的组件,可能有时候我们还要为某个视图添加某些特定组件,例如博客的帖子。解决这个问题的一个方法是创建 const PostLayout = ({component: Component,...rest}) => { return ( <DefaultLayout {...rest} component={matchProps => ( <div className="Post"> <div className="Post-content"> <Component {...matchProps} /> </div> <div className="Post-aside"> <SomeSideBar /> </div> </div> )} /> ); }; <PostLayout path="/posts/:post" component={PostComponent} /> 唯一的区别是将函数传递给 仅此而已短小精悍。 React Router 的新版本专注于使用 React 组件模型,而这些组件可以非常简单的组合在一起。 查看这个 GitHub issue,可以看看对于使用不同的默认布局的讨论。 React Router V4 文档中文翻译 正在进行中。。。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |