Proposal: Remove `children` prop, add prop about when to render
See original GitHub issueI’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:
- Created 6 years ago
- Reactions:36
- Comments:28 (14 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Would rather not have potential props conflict confusion:
So I’d prefer the @edygar suggestion:
<Route when={'miss' | 'match' | 'always'} render={({ match, route }) => } />And default to
alwaysand amatchcheck could be made as suggested by @klznsBenefit of making it something which has to be
true: