使用React-router v4向子组件传递props

4

之前在 react-router v3.* 中,我通过 props 向子组件传递数据:

React.cloneElement(this.props.children, this.props)

在React-router v4中,如何使用新的<Match /> API进行操作?

到目前为止,我想到的解决方法是使用<Match /> API中的render方法:

<Match pattern="/" render={() => <ChildComponent {...this.props} />} />

使用ES6展开语法将props传递给子组件。是否有更好的方法,可以同时将所有标准props(位置,模式,路径名,isExact)传递给子组件?
1个回答

3

根据v4.0.0-alpha5的渲染代码,你有以下两个选择:

<Match pattern="/" render={props => <ChildComponent {...props} {...this.props} />} />
<Match pattern="/">{({matched, ...props}) => {
    return matched ? <ChildComponent {...props} {...this.props} /> : null;
}}</Match>

另外请查看匹配API文档

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Shadrech
似乎 alpha-2 中的相关渲染代码与之相同。但是,我认为 v4 仍然非常实验性。如果您正在寻找稳定的东西,我会使用 v3。 - Alexander Gundermann
是的。我没有意识到 props 是传递给渲染函数的参数。谢谢!这可能是目前最好的解决方案。 - Shadrech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接