2

I have the following route:

<Route path="/userstream/:user" component={ Profile } param="stream" />

Then in my component i get :user in this way: this.props.match.params.user

But, how I can get stream from hardcoded param?

Thank you in advance.

1 Answer 1

5

One way would be to pass it as a prop to component:

<Route path="/userstream/:user" component={ (props) => <Profile {...props} param="stream" /> }  />

Edit: for completeness sake, it's worth to mention a second approach, which is to use a render prop:

<Route path="/userstream/:user" render={ (props) => <Profile {...props} param="stream" /> }  />

There are some performance differences between the two approaches: react router difference between component and render.

Sign up to request clarification or add additional context in comments.

5 Comments

@apokryfos not necessarily.
Maybe I'm using an older version
I mean both should work, I just prefer this version, since it's less verbose.
No I mean I think in older versions of react-router this didn't work. I'm not entirely sure but that's what I remember.
That must be quite an old version, it's been around for a while. Also it seems there are some performance diffs between the two approaches: stackoverflow.com/questions/48150567/…

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.