我的目标是使用以下代码将一个值从一个组件传递到另一个组件:假设我在example.com/iamHere,然后转到example.com/anotherpage,我想将this.state.myprop的值传递到另一个路由。
我正在做一个有一些现有代码的项目,我有这个:
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;
}
});
};
我需要传递一个道具,就像这样的例子:
<Route
path={prop.layout + prop.path}
component={prop.component}
key={key}
myProp={this.state.myProp}
/>
注意添加了myprop,我希望将这个值传递到它要到达的下一个路由。根据上的一篇帖子
git hub
. 就像我在上面的儿童路线我会用这个.props.route.myprop。但这不起作用,它似乎是未定义的。
有人建议:
<Route exact path="/" render={props => <Component {...props} something={this.state.myProp}/>}/>
. 但是如何将上面代码中列出的动态路由组件传递给它呢?
你知道我怎样才能做到吗?