Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easily "Passing Props" from Route to its Component #4967

Closed
wants to merge 1 commit into from

Conversation

Manuelandro
Copy link

There are a lot of request about passing props from Route to Component (ex #615 (comment))
This provide access to everything we pass from the Route component without broken its kinda of render

There are a lot of request about passing props from Route to Component (ex remix-run#615 (comment))
This provide access to everything we pass from the Route component without broken its kinda of render
@Manuelandro Manuelandro changed the title Update Route.js Easily "Passing Props" from Route to its Component Apr 13, 2017
@pshrmn
Copy link
Contributor

pshrmn commented Apr 13, 2017

The current approach is deliberate. If you need to pass props to the component, you should use render instead of component.

@timdorr timdorr closed this Apr 13, 2017
@Manuelandro
Copy link
Author

Manuelandro commented Apr 13, 2017

The current approach is deliberate. If you need to pass props to the component, you should use render instead of component.

@pshrmn
This is not really useful when you have to deal with something like dynamic components
Just a case for example:

{routes.map(props => ( <Route {...props} /> ))}

@hamedmam
Copy link

#4105 (comment)

I found this as a right approach in Router 4.0 but I couldn't get it to work since I am using Link and I am trying to pass props from the component that Link exists in to the path !
Can someone give me an example to what I am trying to do

@Macmee
Copy link

Macmee commented Apr 15, 2017

You can do this:

<Route exact path="/abc" render={props => <TestWidget someProp="2" {...props} />} />

and what I do is I actually spread {...props.match.params} because then on TestWidget you can access the URL parameters with just this.props.urlVariableHere as opposed to this.props.match.params.urlVariableHere for a route setup like /abc/:someUrlQueryBasedVariable/morestuff.

(note to googlers; the above works on v4)

My opinion on the above is that this is very ugly. I would prefer if React directly forwarded on properties assigned to Route straight to the Component itself.

What's more, I dislike how url parameters are stored all the way over in props.match.params, because if you use typescript, then in your Component declaration you cannot have something like:

interface TestWidget {
  someUrlQueryBasedVariable: string
}

You would hope this property would exist at this.props.someUrlQueryBasedVariable but in reality is at this.props.match.params.someUrlQueryBasedVariable which I find very cumbersome to deal with.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants