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

Allow passing props to <Route> to be passed down to the component it renders #5521

Closed
bigblind opened this issue Sep 14, 2017 · 3 comments
Closed

Comments

@bigblind
Copy link
Contributor

The concept

Allow passing props to Route to be passed to the component it renders. For example:

<Route path="/foo" component={MyComponent} foo="bar" />

In that case, if the route matched, MyComponent would be rendered as follows:

<MyComponent match={...} history={...} foo="bar" />

Or, if you don't like Route accepting arbitrary properties, maybe we can achieve the same with something like:

<Route path="/foo" component={MyComponent} extraProps={{foo: "bar"}} />

Use case

Sometimes, I have a component that I want to render in a Route, but make it reusable in such a way that it doesn't have to know about the URL structure that it is in. For example, consider a Twitter clone's UserProfile component:

// Somewhere in the app:
<Route path="/users/:userId" component={UserProfile} />

let UserProfile = ({match}) => {
  return <div>
    {/* ... user profile stuff ... */}
    <Route path={match.path + "/followers"} component={UserFollowers} />
  </div>
}

The UserFollowers component needs access to the userId of the user to show their followers, and it can get that from match.params, but what if we also have some kind of dashboard that displays the currently logged-in user's followers?

// Somewhere in the app:
<Route path="/dashboard/" component={Dashboard} />

let Dashboard = ({match}) => {
 // ... Somehow obtain the userId
  return <div>
    {/* ... dashboard stuff ... */}
    <Route path={match.path + "/followers"} component={UserFollowers} />
  </div>
}

In order to pass the userId variable to UserFollowers, I'd need to use render, and create a whole new function, just to pass the prop to my component. Or, I'd have to use a HOC like recompose's withProps. Both of these options work, but I think it'd be more elegant to just have a way to pass props through via Route.

@timdorr
Copy link
Member

timdorr commented Sep 14, 2017

This has been discussed many, many, many, many times.

The simplest solution is just to use render instead:

<Route path="/abc" render={()=><TestWidget num="2" someProp={100}/>}/>

@cakraww
Copy link

cakraww commented May 22, 2018

<Route path="/abc" render={(props) => <TestWidget {...props} someProp={100} />} />
is more complete as it forwards the default props as well (e.g: match prop).

@moparlakci

This comment has been minimized.

@remix-run remix-run locked and limited conversation to collaborators May 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants