gpt4 book ai didi

reactjs - 在react-router 2.4中将props传递给路由

转载 作者:行者123 更新时间:2023-12-03 14:01:30 25 4
gpt4 key购买 nike

我想将一些 Prop 从路由器传递到组件

类似这样的事情:

<Route component={Foo} some_prop={12}/>

有没有办法用最新的react-router来做到这一点?我知道在 1.0 之前这是不可能的(不使用包装器)。

最佳答案

如果您将它们直接传递到 <Route />像这样的组件,现在可以完全按照您建议的方式进行操作:

<Route foo="bar" />

将使您能够访问foo支持this.props.route.foo

通过:https://github.com/reactjs/react-router/blob/master/docs/Troubleshooting.md#passing-additional-values-into-route-components

作为旁注:如果您使用的是更高级别的内容,例如 <RouterContext />不幸的是,仍然需要使用某种数据包装器组件。

关于reactjs - 在react-router 2.4中将props传递给路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37400175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com