使用React Router v4传递道具组件

时间:2017-05-26 14:15:29

标签: reactjs meteor

我想创建文章页面,链接在博客列表中。我的路线:

<Route path = '/blog/:slug' component={Article} />

我不知道如何将道具数据(标题,描述等)传递给我的文章组件。我在使用MeteorJS项目,我的数据不是静态的,而是MongoDB。

你知道我是否可以传递全局对象? :

<Route path = '/blog/:slug' component={Article} data={props} />

任何人都可以帮助我吗? :)

谢谢社区!

1 个答案:

答案 0 :(得分:2)

您可以使用内联render功能代替component并照常传递道具。

例如,如果您想将标题道具传递给文章组件,您可以执行以下操作。

<Route path = '/blog/:slug' render={(props)=><Article title="My article title" {...props}/>} />

这里props是来自路由器的道具,如匹配,位置,历史和staticContext。如果您在文章组件中不想要它们,则可以删除{...props}

相关问题