[英]React Router dynamic pass down props
My goal is to pass a value from one component to another with the following code: So let's say I'm at example.com/iamhere and I go to example.com/anotherpage I would like to pass the value of this.state.myProp to that other route. 我的目标是使用以下代码将值从一个组件传递到另一组件:假设我在example.com/iamhere,然后转到example.com/anotherpage,我想将this.state的值传递给我。 myProp到另一条路线。
I'm working on a project with some existing code and I have this: 我正在一个有一些现有代码的项目中,我有这个:
getRoutes = routes => {
return routes.map((prop, key) => {
if (prop.layout === "/admin") {
return (
<Route
path={prop.layout + prop.path}
component={prop.component}
key={key}
myProp={this.state.myProp}
/>
);
} else {
return null;
}
});
};
I need to pass a prop down, just as an example something like this: 我需要传递一个道具,就像这样的例子:
<Route
path={prop.layout + prop.path}
component={prop.component}
key={key}
myProp={this.state.myProp}
/>
Notice the addition of myProp I wish to pass this value to the next route it goes to. 请注意,我希望将myProp的值传递给它去的下一个路由。 According to a post on git hub .
根据git hub上的帖子。 It would be like I did above then in child route I'd use this.props.route.myProp.
就像我在上面所做的那样,然后在子路径中使用this.props.route.myProp。 However that doesn't work, it appears as undefined.
但是,这不起作用,它显示为undefined。
This has been suggested: 有人建议:
<Route exact path="/" render={props => <Component {...props} something={this.state.myProp}/>}/>
But how can I pass the dynamic route component listed in the above code to it? 但是如何将上面代码中列出的动态路由组件传递给它呢?
Any idea how I can achieve it? 知道如何实现吗?
一种选择是使用React Router的render
prop而不是component
:
<Route exact path="/" render={props => <Component {...props} something={this.state.myProp}/>}/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.