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.

ThemeProvider doesn't work as expect

See original GitHub issue

From the documentation

In the render tree all styled-components will have access to the provided theme, even when they are multiple levels deep.

So I did this https://github.com/straw-hat-team/impetus_bit/blob/master/src/OTP/Core/Application.js#L40

render() {
    const shouldShowLoadingScreen = this.showLoadingScreen()
    const children = shouldShowLoadingScreen ? this.renderLoadingScreen() : this.renderBody()

    return (
      <Provider applicationStore={this.store}>
        <IntlProvider locale={navigator.language}>
          <ThemeProvider theme={Theme}>
            <BaseHold baseline={Theme.baseline} ratio={Theme.ratio} unit={Theme.unit}>
              {children}
            </BaseHold>
          </ThemeProvider>
        </IntlProvider>
      </Provider>
    )
  }

So notice I add my ThemeProvider but when I do console.log(this.props.theme) doesn’t show any theme key

Expected Behavior

I should see theme key in the props in all the components from that HoC in.

Actual Behavior

I don’t see the theme key in the props

Issue Analytics

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

github_iconTop GitHub Comments

35reactions
mxstbrcommented, Dec 2, 2017

“In the render tree all styled-components will have access to the provided theme, even when they are multiple levels deep.”

Note how it says “styled-components” and NOT “react components”. Only all styled-components have access to the theme, like so:

const SomeComp = styled.div`
  colors: ${props => props.theme.primary};
`

If you want to get the theme in your normal React component you have to use our withTheme higher order component:

import { withTheme } from 'styled-components'

class SomeComp extends React.Component {
  render() {
    console.log(this.props.theme);
  }
}

export default withTheme(SomeComp);

Next time when you have a usage question please ask in the community or on StackOverflow, this is an issue tracker. Thanks!

1reaction
mxstbrcommented, Dec 2, 2017

This is likely a usage problem, please post the code where you do console.log(this.props.theme).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is ThemeProvider (Material UI) not working for me here?
Checking the documents of material-ui it turns out you have imported some of the things from library in a wrong way. Like the...
Read more >
Theming with React and MUI - Welcome, Developer
MUI provides a ThemeProvider component that provides the theme to all the UI components in the app. This component expects a theme object...
Read more >
Advanced (LEGACY) - MUI System
Add a ThemeProvider to the top level of your app to pass a theme down the React component ... Applying withStyles(styles) as a...
Read more >
API Reference - styled-components
ThemeProvider. A helper component for theming. Injects the theme into all styled components anywhere beneath it in the component tree, ...
Read more >
Troubleshooting | React Navigation
If the module points to an npm package (i.e. the name of the module doesn't with ./ ), then it's probably due to...
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