React Router V4 - 无法在 props 中获取 URL 参数

Mar*_*ano 0 reactjs react-router react-router-v4

我正在尝试创建简单的 React-Redux-Router V4 应用程序。到我的组件之一的路由使用了 url 参数。在我的Route组件中,我一直在传递renderprop,它是一个返回组件的匿名函数。就像这样:

<Route key="post" path="/post/:id" render={() => <Post/>} />

我使用这个道具而不是道具component

<Route key="post" path="/post/:id" component={Post} />

因为我无法使用componentprop 将 props 添加到渲染的组件中。问题是,使用componentprop,您可以使用props.match.params.name,它将包含我需要的确切参数。使用该组件时此功能不可用render

可以用window.location.href,但是感觉很脏。

谢谢!

tec*_*him 5

、和props 被传递到您的match函数中。locationhistoryrender

例子:

<Route key="post" path="/post/:id" render={({ match }) => <Post name={match.params.name}/>} />
Run Code Online (Sandbox Code Playgroud)

文档:https ://reacttraining.com/react-router/web/api/Route/Route-props