question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide Implementation to pass props to routed components without render()

See original GitHub issue

After reading all of the discussion in #4942, the resolution to use render() to pass props to route components leaves devs who hope to use the now standard react/redux with some unfortunate tradeoffs. The reality of using redux with react means that you will end up with a few top-level connected components with many dumb children. A common pattern evolves wherein router definitions generally happen in connected components, which results in extensive use of render() to pass props to dumb components.

In my current project, the component prop only gets used a few times, on the top-level routers. The bulk of my routing declarations are using render, which ends up being a lot of annoying boilerplate and very verbose routing declarations. I propose this syntax, (IE the original syntax), for passing props to routed components:

<Route component={Component} foo={bar} />

Resulting in {foo: bar} as a prop on the rendered Component. I’m sure there were good reasons for the split between component and render, but because passing props to routed components ends up being the vast majority of use cases in large projects, devs end up using the counterintuitive and verbose render() method everywhere, when the older syntax would have served better.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
timdorrcommented, Jul 31, 2017

This has been discussed many, many, many times.

This is all you need:

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

I’m not following your logic as to why that is an unworkable API. Perhaps an example makes that more clear. But other than looking different and involving a few more keystrokes, I don’t see how it’s not tenable.

8reactions
fungiboletuscommented, Aug 18, 2017
<Route path="/abc" render={()=><TestWidget num="2" someProp={100}/>}/>

It could be helpful to have this example in a good place in the documentation. It’s difficult to find the correct way to do it with the latest release, the stackoverflows comments or github’s request are often a bit old.

Also, this is quite ugly but who cares…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Provide Implementation to pass props to routed components ...
render feature appears in 4.x . while react-router-redux only works with 3.x or former. So we should use function component currently?
Read more >
Props! — and how to pass props to components in React ...
The route props include match, location, and history which are used to get the current route state from React Router within your component....
Read more >
How to pass additional props throughout a Route element that ...
I want to be able to pass some additional prop we need in this component. And also we need to use the Location,...
Read more >
How to Pass Props to a Component Rendered by React Router
In this up-to-date guide, you'll learn everything you need to know about passing props to a component rendered by React Router.
Read more >
Render Props - React
A component with a render prop takes a function that returns a React element and calls it instead of implementing its own render...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found