reactjs – 将道具传递给React-Router 4.0.0儿童路由
发布时间:2020-12-15 20:54:19 所属栏目:百科 来源:网络整理
导读:我正在尝试将现有的react-router 3.0.0应用程序迁移到 4.0.0.在路由器中,我需要向其子节点传递一个额外的非路由相关的prop. 在以前版本的react-router,detailed in this post中有一些有点hacky方法可以实现这一点.我个人最喜欢的是使用cloneElement的this me
我正在尝试将现有的react-router 3.0.0应用程序迁移到
4.0.0.在路由器中,我需要向其子节点传递一个额外的非路由相关的prop.
在以前版本的react-router,detailed in this post中有一些有点hacky方法可以实现这一点.我个人最喜欢的是使用cloneElement的this method,因为它确保所有的react-routers道具都被复制了: // react-router 3.0.0 index.js JSX <Router history={browserHistory} <Route path="/" component={App}> <Route path="/user/:userId" component={User} /> <IndexRoute component={Home} /> </Route> </Routes> // App component JSX (uses cloneElement to add a prop) <div className="App"> { cloneElement(props.children,{ appState: value }) } </div> 到目前为止,我的新App组件如下所示: // react-router 4.0.0 App component JSX (moved routing code out of index.js and into here) <BrowserRouter> <div className="App"> <Match exactly pattern="/" component={Home} /> <Match pattern="/user/:userId" component={User} /> <Miss component={Home} /> </div> </BrowserRouter> 在保留提供的路由器道具的同时,为每个Match的组件添加appState prop的最佳方法是什么?
<路线>可以采用渲染道具而不是组件.这允许您内联组件定义.
<Route path='/user/:userId' render={(props) => ( <User appState={value} {...props} /> )} /> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 分析Ajax来爬取今日头条街拍美图并保存到MongDB
- ruby-on-rails – ActiveAdmin自定义选择过滤器下拉列表名称
- flex4解决label中文字过长显示省略号的办法
- postgresql – PostGIS:从其他点查找特定半径范围内的点
- Cocos2d-x v3.X的颜色混合BlendFunc使用详解
- How To Read and Write XML Documents with GDataXML
- cocos2d学习之CCLabelTTF
- c# – 迁移到.Net 4:在xaml中添加事件时抛出空引用异常
- 左侧后增量
- ruby-on-rails – Erlang是否有Sinatra风格的网络框架?