3

I am new to redux, I created a route to navigate to user edit page and i also want to pass store and history as props using the code below.

<Route path="/edit/:id" render={({history}) => (
          <EditUser store={store} history={history} />
)}/>

But now i cannot access the params which i am passing (:id) in my EditUser component. When i try to access using this.props.match.params.id, it shows undefined, but it works if i rewrite my route to

<Route path="/edit/:id" component={EditUser}/>

But now i think case i am not able to pass other props.

Help much appreciated.

2
  • which <Route> are you using <BrowserRouter> , share your index.js? Commented Nov 17, 2017 at 19:18
  • yes i am using <BrowserRouter> in my index.js Commented Nov 17, 2017 at 19:25

1 Answer 1

5

If you want to use match you have to pass it to your component.

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

You could also just pass all the props:

<Route path="/edit/:id" render={props => (
  <EditUser store={store} {...props} />
)}/>

Or you can use react-redux's connect function to get access to the store

<Route path="/edit/:id" component={connect(mapStateToProps)(EditUser)}/>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this awesome answer with multiple ways of wiring react-router with react-redux. I can't believe this isn't more obvious in the docs...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.