使用render()时REACT路由器无法访问参数

Aru*_*run 3 reactjs react-router react-router-v4 react-props

我是redux的新手,我创建了一条导航到用户编辑页面的路线,并且我还想使用以下代码将存储和历史记录作为道具传递。

<Route path="/edit/:id" render={({history}) => (
          <EditUser store={store} history={history} />
)}/>
Run Code Online (Sandbox Code Playgroud)

但是现在我无法访问在EditUser组件中传递(:id)的参数。当我尝试使用this.props.match.params.id进行访问时,它显示为undefined,但是如果我将路由重写为

<Route path="/edit/:id" component={EditUser}/>
Run Code Online (Sandbox Code Playgroud)

但是现在我认为我无法通过其他道具。

帮助非常感谢。

azi*_*ium 5

如果要使用match,则必须将其传递给组件。

<Route path="/edit/:id" render={({history, match}) => (
  <EditUser store={store} history={history} match={match} />
)}/>
Run Code Online (Sandbox Code Playgroud)

您还可以通过所有道具:

<Route path="/edit/:id" render={props => (
  <EditUser store={store} {...props} />
)}/>
Run Code Online (Sandbox Code Playgroud)

或者您可以使用react-redux的connect功能来访问商店

<Route path="/edit/:id" component={connect(mapStateToProps)(EditUser)}/>
Run Code Online (Sandbox Code Playgroud)