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.

Allow withTheme to have a fallback theme when rendered outside of a ThemeProvider

See original GitHub issue

Currently, 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:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:33 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
mxstbrcommented, Apr 1, 2017

I think this isn’t an issue with testing, but with inconsistency:

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

I agree that this is weird, and would appreciate a PR adding the behaviour from the styled components to withTheme so it’s consistent!

2reactions
brunolemoscommented, Mar 18, 2017

ThewithTheme HoC is highly attached to ThemeProvider because it doesn’t make sense to use withTheme 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.

Read more comments on GitHub >

github_iconTop 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 >

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