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

Add "props" as an attribute when configuring a Route #3707

Closed
atrauzzi opened this issue Aug 5, 2016 · 9 comments
Closed

Add "props" as an attribute when configuring a Route #3707

atrauzzi opened this issue Aug 5, 2016 · 9 comments

Comments

@atrauzzi
Copy link

atrauzzi commented Aug 5, 2016

Plain and simple. Whatever gets passed in here is like as if they were defined as props on the routed component itself.

Reason: My components shouldn't need to be aware of react router conventions just to be passed parameters from wherever they're being called.

Example:

<Router>
    <Route path="/some/path" props={myBagOfData} component={UnawareOfReactRouter} />
</Router>

Can't call it props? Call it passthroughProps, the name doesn't matter. The concept here is that it needs to be easier to preserve standard react conventions without creating a two way coupling between routed components and react-router. Which is what happens when you make the expectation that components now have to access this.props.route to get at any values.

@taion
Copy link
Contributor

taion commented Aug 5, 2016

You can trivially build an HoC that spreads the properties of this.props.route into the child's props. You can even use a custom route component that automatically applies the HoC.

I don't think it makes sense to explicitly bring this functionality into the router core.

@taion taion closed this as completed Aug 5, 2016
@atrauzzi
Copy link
Author

atrauzzi commented Aug 5, 2016

@taion I think you're mistaken. Your suggestion forces people to write boilerplate, essentially offloading extra work created by this library onto it's consumers.

Why should I take on the overhead of writing wrappers for each individual component I need to use?

@atrauzzi
Copy link
Author

atrauzzi commented Aug 5, 2016

For what it's worth, I didn't open this ticket without doing prior research... I'm not the first person to need this and it's not exactly been clear over react-router's history as to what the best practice is.

(I don't want to spend my day producing links about this, but I'm sure you get the idea)

A higher order component seems like overkill given that most of the time people just want the props passed down 1:1. So why not include it, or include the "custom route component that automatically applies the HoC"? There's a lot of wheel reinventing going on here I bet...

@taion
Copy link
Contributor

taion commented Aug 5, 2016

The HoC is literally just:

const withRouteProps = Component => (...props) => (
  <Component {...props} {...props.route.props} />
)

The cost to you in adding that is trivial. The cost to us to adding that is expanding the API surface area in an arbitrary way that doesn't actually allow something new, or allow a meaningful change to UX.

Think about it this way. If you want to interact with things like params, you need to map those to locally significant prop names anyway. If reusing higher-level components is important to you, then you should think of your route components as doing that mapping of e.g. params.foo to just foo.

In which sense this is design-wise the wrong thing to ask for.

@ptica
Copy link

ptica commented Aug 24, 2016

The cost to you in adding that is trivial.

really, for a newcomer not trivial: 20min reading various stackoverflow/github issues about just passing the dammed props, +20min for grokking the HoC :(

@taion
Copy link
Contributor

taion commented Aug 24, 2016

Then just use props.route.whatever!

@tj
Copy link

tj commented Oct 5, 2016

I didn't realize how hard this was, I'm not using Redux in this project so it's really apparent now, had to do a ton of searching as well. There's no way something like this would get approved? Everything else feels like a huge hack for stateless apps.

@ryanflorence
Copy link
Member

this isn't a pain in v4, just FYI.

@tj
Copy link

tj commented Oct 5, 2016

Nice yea I still have to check that out! Rocking the old style for now. Looks like route will do the trick! Google turns up a ton of crufty hacks but that's not too bad.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 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

No branches or pull requests

5 participants