Allow withTheme to have a fallback theme when rendered outside of a ThemeProvider
See original GitHub issueCurrently, when trying to test or use a component wrapped with withTheme when no ThemeProvider is found in the context, it will throw an error in componentWillMount.
[withTheme] Please use ThemeProvider to be able to use withTheme
As a result, if we want to mount a component wrapped with “withTheme” for testing - we have to wrap it manually with <ThemeProvider theme={{}}> <Component /> </ThemeProvider>
to test it properly.
Was it a design decision to always require ThemeProvider to be in the tree to use withTheme? It seems odd to have such a strict requirement on this, since the styled
components don’t
If not, I see two different solutions to this, either:
- Simply set the theme to be an empty object, when not found
- Allow specifying a default theme to use in the absence of a ThemeProvider when using withTheme (which could just default to an empty object, handling the previous solution by default). I’m not sure how much of a need this is
I’d be interested to hear your thoughts either way, and I’d be happy to lend a hand making any changes if you think this is something worth pursuing
Issue Analytics
- State:
- Created 7 years ago
- Reactions:5
- Comments:33 (15 by maintainers)
Top Results From Across the Web
Is ThemeProvider necessary for theming in styled components?
Any changes in your theme will not be reflected in your components, because they don't re-render if external values change. · In every...
Read more >Advanced Usage - styled-components
In the render tree all styled-components will have access to the provided theme, even when they are multiple levels deep. To illustrate this,...
Read more >Styles API - MUI System
It will be linked to the component. Use the function signature if you need to have access to the theme. It's provided as...
Read more >Theming | Amplify UI for React
An Amplify UI theme is a structured object of design tokens, breakpoints, ... You can always fall back to writing CSS (or a...
Read more >How to use Themes in styled-components - DEV Community
It also allows me to write way less code and be more consistent with my styling. By leveraging the theme provider in styled-components...
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 FreeTop 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
Top GitHub Comments
I think this isn’t an issue with testing, but with inconsistency:
I agree that this is weird, and would appreciate a PR adding the behaviour from the styled components to
withTheme
so it’s consistent!The
withTheme
HoC is highly attached toThemeProvider
because it doesn’t make sense to usewithTheme
without it.@5id @kopax can’t you just wrap
<ThemeProvider>
in your tests? That’s the way you application code is, so that’s the way you should be testing it.