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.

Proposal: Remove `children` prop, add prop about when to render

See original GitHub issue

I’d like to change how Route render and children work.

What’s the difference between them?

<Route path="/foo" children={() => (
  <div>Children always renders, match or miss.</div>
)}>

<Route path="/foo" render={() => (
  <div>Render only renders if there's a match, null if miss</div>
)}>

The API isn’t instructive

The API is not instructive of behavior at all: “render” doesn’t indicate “only renders when there’s a match” and children doesn’t indicate anything either.

Maybe we can ditch having two render props with a prop that is instructive of the behavior.

<Route alwaysRender={true} render={({ match }) => (
  <div>Now we only have one render prop, and an instructive API</div>
)}/>

I’m pretty convinced this is a good way forward. Route has two types of props:

  • How should I match? (path, exact, strict)
  • How should I render? (render, children, component)

It’s a bit conflated that the distinction between render and children is how things match!

Component prop

Also, with this change the component prop can participate in use-cases where you want to render even when there’s no match. Today you have to use the children prop.

<Route children={(props) => <OtherThing {...props} />}/>
// becomes this when the use case for the route changes:
<Route alwaysRender={true} component={OtherThing}/>

Bikeshedding on the name of the prop

<Route alwaysRender={false} render={() => ()}/>
<Route renderOnMiss={false} render={() => ()}/>
<Route renderOnMatchOnly={true} render={() => ()}/>
<Route renderOn={"match" || "miss" || "both"} render={() => ()}/>
<Route ignoreMatch={false} render={() => ()}/>

Thoughts?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:36
  • Comments:28 (14 by maintainers)

github_iconTop GitHub Comments

6reactions
approotscommented, Oct 3, 2017

Would rather not have potential props conflict confusion:

<Route renderAlways={() => ()} renderMiss={() => ()}/>`
<Route always miss render={() => ()} />

So I’d prefer the @edygar suggestion:

<Route when={'miss' | 'match' | 'always'} render={({ match, route }) => } />

And default to always and a match check could be made as suggested by @klzns

5reactions
insincommented, Oct 3, 2017

Benefit of making it something which has to be true:

<Route always render={() => ()}/>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove `children` prop, add prop about when to render ...
I'd like to change how Route render and children work. What's the difference between them? ( Children always renders, match or miss. )...
Read more >
How to pass props to {this.props.children} - Stack Overflow
Cloning children with new props. You can use React.Children to iterate over the children, and then clone each element with new props (shallow...
Read more >
React Children And Iteration Methods - Smashing Magazine
In this article, we'll discuss and learn about the use case of iterating over React `children` and the ways to do it. In...
Read more >
Building React Components Using Children Props ... - Soshace
React provides a number of powerful patterns to compose components; for example, Containment, Specialization, and Render Props.
Read more >
Everything you need to know about the children prop in React
In this article, we will see the importance of children prop. So let's get started. When we render any JSX, React converts that...
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