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.

Remove React.Children.only from theme provider

See original GitHub issue

It there a reason to have React.Children.only in the theme provider? It seems unnecessary and it makes certain layouts not possible. Why not simply return this.props.children from render?

Issue Analytics

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

github_iconTop GitHub Comments

7reactions
mattlubnercommented, Mar 12, 2019

Because the <ThemeProvider /> can be at the top of the component tree, near your ReactDOM.render.

🤔 @mxstbr I do not believe this is a very compelling reason for <ThemeProvider /> to be prescriptive about the shape of its children. By this logic, all components that theoretically could, but not necessarily will be placed at the top level of a React application should be restricted to a single child. I hope we can all agree that that would be silly!

Also, we shouldn’t be cavalier about inserting unnecessary <div />s either, as it can break all sorts of different layouts (flexbox, child selectors, etc). There are numerous existing React applications, which are no doubt using such layouts, that are prevented from using <ThemeProvider />in certain ways or places because of this restriction.

So, this choice is definitely adding complexity to consuming applications, and the trade-off is to… forgive me, but is it just to shelter junior-level developers who don’t yet realize ReactDOM.render() must only have a single top-level node?

It seems like removing this restriction is a win-win: (a) we get reduced complexity, and (b) junior developers aren’t sheltered from learning a fundamental restriction of using React.

If the effort needed is the only argument against lifting this restriction, I would be glad to contribute the necessary PRs! 🤓

7reactions
mxstbrcommented, Nov 25, 2017

I’m sure a single wrapper div is going to kill your apps performance 😉

Nobody’s talking about the code change, of course changing three lines of code is easy. I can do that in two minutes tops.

The problem is what comes afterwards: We gotta update all the documentation and any old blog posts, write a changelog documenting this and the reason for changing it, and once that’s done we gotta deal with the dozens of questions we’ll get about that weird React error when you try rendering a top-level ThemeProvider.

For what?

I don’t even know how you’re using the ThemeProvider, can you please provide the code from your app that is so much harder to write due to our implementation? I’d love to help you resolve that styling issue, because I’m 100% it’s resolvable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - React.Children.only expected to receive a single ...
You can either remove the space: <Provider store={store}><App /></Provider>. Or break it up on to multiple lines:
Read more >
Context - React
Context provides a way to pass data through the component tree without having to pass props down manually at every level. In a...
Read more >
Building React Components Using Children Props ... - Soshace
Besides that, the code is easy to reuse: just wrap the Theme component around any children and they become “themed”.
Read more >
API Reference - styled-components
Any valid React component that can handle a theme prop. Returns the passed component inside a wrapper (higher order component).
Read more >
3 Ways To Theme React Components - Bits and Pieces
This ThemeProvider enables us to provide themes for React components as objects to any hierarchy in our component tree. Let's see an example:...
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