简体   繁体   English

通过道具React Router

[英]Pass props React Router

I need to pass props through React Router to LawsList Component. 我需要将道具通过React Router传递到LawsList组件。 So I come up with this code: 所以我想出了这段代码:

On main.js : 在main.js上:

const lawlistcomponent = props => {
  console.log("test");
  return <LawsList {...props} />;
};

const routes = (
  <Router history={browserHistory}>
    <Route path="/" component={App}>
      <IndexRoute render={lawlistcomponent} />
      <Route path="laws/:lawId" component={LawsMain} />
      <Route path="votes/:lawId" component={VotesMain} />
    </Route>
  </Router>
);

And on LawsList component : 在LawsList组件上:

 export default createContainer(props => {
  console.log(this.props.voted);
  Meteor.subscribe("laws", props.voted);
  return { laws: Laws.find({}).fetch() };
}, LawsList);

It does not return the component at all. 它根本不返回该组件。 I have no error message that could guide either ! 我没有错误消息可以指导!

Any hint ? 有什么提示吗?

Your local lawlistcomponent component is not receiving any props in the way it was passed to the route. 您当地的lawlistcomponent组件没有以传递到路线的方式接收任何道具。 Anyway, I'd recommend you to use the render inline function to render the component to the route (supposing that you are using the React Router V4). 无论如何,我建议您使用render inline函数将组件渲染到路由(假设您正在使用React Router V4)。 Like this: 像这样:

<Route
  exact
  path='/'
  render={(props) => <LawsList {...props} anotherProp={value} />}
/>

As described in the React router docs : 如React 路由器文档中所述

This allows for convenient inline rendering and wrapping without the undesired remounting explained above. 这样可以方便地进行内联渲染和包装,而无需进行上述不必要的重新安装。 Instead of having a new React element created for you using the component prop, you can pass in a function to be called when the location matches. 无需使用组件prop为您创建新的React元素,而是可以传递位置匹配时要调用的函数。 The render prop receives all the same route props as the component render prop. 渲染道具接收与组件渲染道具相同的所有路线道具。

Thanks Jens for your suggestion. 感谢Jens的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM