问题描述
我无法解决 React 路由器的问题.场景是我需要从状态父组件和路由传递子路由一组道具.
我想做的是传递 childRouteA 它的 propsA,并传递 childRouteB 它的 propsB.但是,我能弄清楚如何做到这一点的唯一方法是同时传递 RouteHandler propsA 和 propsB 这意味着每个子路由都会获取每个子路由道具不管它是否相关.目前这不是一个阻塞问题,但我可以看到有一段时间我会使用相同组件中的两个,这意味着 propA 上的键将被 propB 上的键覆盖.
# 个路由路线 = (<Route name='filter' handler={ Parent } ><Route name='price' handler={ Child1 }/><Route name='time' handler={ Child2 }/></路线>)# 父组件渲染:-><RouteHandler {...@allProps()}/></div>时间道具:->富:'酒吧'价格道具:->巴兹:'qux'#assign = 需要'object-assign'allProps:->分配 {}、timeProps()、priceProps()这实际上按我期望的方式工作.当我链接到 /filters/time 时,我得到了 Child2 组件.当我去 /filters/price 我得到 Child1 组件呈现.问题是通过执行此过程, Child1 和 Child2 都被传递 allProps() 即使它们分别只需要价格和时间道具.如果这两个组件具有相同的道具名称,这可能会成为一个问题,并且通常使用不需要的道具来膨胀组件并不是一个好习惯(因为在我的实际情况下有两个以上的孩子).
总之,有没有办法在我去时间路线(filters/time)时通过RouteHandler timeProps 并且只通过priceProps当我转到价格路线(filters/price)时到 RouteHandler 并避免将所有道具传递给所有子路线?
解决方案 我遇到了类似的问题,发现可以通过this.props.route 访问Route上设置的props 在你的路由组件中.知道了这一点,我这样组织我的组件:
index.js
React.render((<路由器历史={new HashHistory()}><路由组件={App}><路线路径="/你好"名称=你好"组件={views.HelloView}水果={['橙子', '香蕉', '葡萄']}/></路线></路由器>), document.getElementById('app'));
App.js
class App 扩展 React.Component {构造函数(道具){超级(道具);}使成为() {返回 <div>{this.props.children}</div>;}}
HelloView.js
class HelloView 扩展 React.Component {构造函数(道具){超级(道具);}使成为() {返回 <ul>{this.props.route.fruits.map(fruit =><li key={fruit}>{fruit}</li>)}</ul></div>;}}这是使用 react-router v1.0-beta3.希望这会有所帮助!
<小时>好的,现在我对您的问题有了更好的理解,您可以尝试以下方法.
由于您的子 props 来自单个父组件,因此您的父组件(而不是 react-router)应该是管理渲染哪个子组件的组件,以便您可以控制传递哪些 props.
您可以尝试更改路由以使用参数,然后在父组件中检查该参数以呈现适当的子组件.
路线
<Route name="filter" path="filter/:name" handler={Parent}/>
父组件
render: function () {if (this.props.params.name === 'price') {返回 <Child1 {...this.getPriceProps()}/>} else if (this.props.params.name === 'time') {返回 <Child2 {...this.getTimeProps()}/>} 别的 {//别的东西}}
I'm having trouble overcoming an issue with react router. The scenario is that i need to pass children routes a set of props from a state parent component and route.
what i would like to do is pass childRouteA its propsA, and pass childRouteB its propsB. However, the only way i can figure out how to do this is to pass RouteHandler both propsA and propsB which means every child route gets every child prop regardless of whether its relevant. this isnt a blocking issue at the moment, but i can see a time when i'd be using the two of the same component which means that keys on propA will overwritten by the keys by the keys of propB.
# routes
routes = (
<Route name='filter' handler={ Parent } >
<Route name='price' handler={ Child1 } />
<Route name='time' handler={ Child2 } />
</Route>
)
# Parent component
render: ->
<div>
<RouteHandler {...@allProps()} />
</div>
timeProps: ->
foo: 'bar'
priceProps: ->
baz: 'qux'
# assign = require 'object-assign'
allProps: ->
assign {}, timeProps(), priceProps()
This actually works the way i expect it to. When i link to /filters/time i get the Child2 component rendered. when i go to /filters/price i get the Child1 component rendered. the issue is that by doing this process, Child1 and Child2 are both passed allProps() even though they only need price and time props, respectively. This can become an issue if those two components have an identical prop name and in general is just not a good practice to bloat components with unneeded props (as there are more than 2 children in my actual case).
so in summary, is there a way to pass the RouteHandler timeProps when i go to the time route (filters/time) and only pass priceProps to RouteHandler when i go to the price route (filters/price) and avoid passing all props to all children routes?
解决方案 I ran into a similar issue and discovered that you can access props set on the Route through this.props.route in your route component. Knowing this, I organized my components like this:
index.js
React.render((
<Router history={new HashHistory()}>
<Route component={App}>
<Route
path="/hello"
name="hello"
component={views.HelloView}
fruits={['orange', 'banana', 'grape']}
/>
</Route>
</Router>
), document.getElementById('app'));
App.js
class App extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div>{this.props.children}</div>;
}
}
HelloView.js
class HelloView extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div>
<ul>
{this.props.route.fruits.map(fruit =>
<li key={fruit}>{fruit}</li>
)}
</ul>
</div>;
}
}
This is using react-router v1.0-beta3. Hope this helps!
Ok, now that I'm understanding your issue better, here's what you could try.
Since your child props are coming from a single parent, your parent component, not react-router, should be the one managing which child gets rendered so that you can control which props are passed.
You could try changing your route to use a param, then inspect that param in your parent component to render the appropriate child component.
Route
<Route name="filter" path="filter/:name" handler={Parent} />
Parent Component
render: function () {
if (this.props.params.name === 'price') {
return <Child1 {...this.getPriceProps()} />
} else if (this.props.params.name === 'time') {
return <Child2 {...this.getTimeProps()} />
} else {
// something else
}
}
这篇关于将 props 传递给 React Router 子路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22
前端开发问题
182
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18
前端开发问题
301
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17
前端开发问题
437
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11
前端开发问题
455
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20
前端开发问题
5
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20
前端开发问题
5
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)