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.

styled-components' context doesn't work

See original GitHub issue

šŸ’¬ Questions and Help

When I wrap <Component /> with a Provider <ThemeProvider theme={theme} > in _app and then try to consume this theme or provider value it returns undefined instead of the provider value.

Please help, I have been 2 days debuging to find the issue and there is no luck

home.test.jsx

it('should renders home page', async () => {
    const { render} = await getPage({
         route: '/home',
          useApp: true
    });
    render();
    expect(screen.getByText('home'))
    expect(screen.getByText('child'))
});

_app.js

import React, {useContext} from 'react';
import styled, { ThemeProvider, ThemeContext } from 'styled-components';
import ThemJson from "../theme";

const StyledContainer = styled.div`
    background-color: ${props => { 
        console.log("PROPS IN STYLED COMPONENTS", props.theme);  // output is correct
        return props.theme.color.primary;
    }
};
`
const AppChild = () => {
    const theme = useContext(ThemeContext);
    console.log(theme) // Output correct
    return (<StyledContainer>home</StyledContainer>);
};

const MyApp = ({ Component }) => {
  return (
      <ThemeProvider theme={ThemJson}>
         <AppChild />
         <Component />
    </ThemeProvider>
  );
};
export default MyApp

home.jsx

import React, {useContext} from 'react';
import styled, {ThemeContext} from 'styled-components';

export const StyledContainer = styled.div`
    background-color: ${props => {
        console.log("PROPS IN STYLED COMPONENTS", props.theme); // ERROR can't red property theme of undefined
        return props.theme.color.primary;
    }
};
`

function Home(props) {
    const theme = useContext(ThemeContext);
    console.log(theme) // Wrong!! // theme is undefined
    return (
        <StyledContainer>
            child
        </StyledContainer>
    );
};

export default Home;

theme.json

{
    "color": {
        "primary": "red"
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:23

github_iconTop GitHub Comments

5reactions
Meemawcommented, Feb 3, 2021

@toomuchdesign I was just trying to make this work locally and I failed. I think its clear why the theme is undefined in ā€œserverā€ context if styled-components is not listed in nonIsolatedModules option.

However if it’s listed in nonIsolatedModules there will be a runtime error during SSR rendering. The problem is what if we list a module inside nonIsolatedModules it means that module becomes a client side module and can’t be ā€œisolatedā€. This is problematic because then it wont work correctly in ā€œserverā€ context due to such expressions: https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/constants.js#L15 and fail at SSR render time.

As I see it its currently not possible to test applications that use styled-components at all. This is not good as this represents a big number of users.

While I don’t see short term solutions to our ā€œclientā€ vs ā€œserverā€ execution context problem (e.g. rendering in separate processes), I think this (and probably other use cases) would be possible if we had a ssr: boolean option. If that option is disabled:

ssr: false

We wouldn’t do the server render + hydrate, but just do the client side rendering. I quickly tried that locally and the styled-components example seemed to work.

1reaction
Meemawcommented, Feb 25, 2021

@ThePicoNerd I have a feeling you need to patch-jest; or have you done that already?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Usage - styled-components
This component provides a theme to all React components underneath itself via the context API. In the render tree all styled-components will have...
Read more >
Styled-Components - Style doesn't work in hover context
I'm having some issues trying to set a different color to the background of a button depending of an attribute. Doing something like...
Read more >
Styled Components - Theme UI
Styled Components. Theme UI itself doesn't expose an API for styled components, but works seamlessly with the styled API from the @emotion/styled package....
Read more >
Using Styled Components with React Native - Level Up Coding
Open up App.js . Declare a new Container View using styled . Inside the backticks, you can put pure CSS code there with...
Read more >
Adding themes to a React app using styled-components | Blog
Providing more than one theme option gives these users a chance to try out different themes and see what works best for them....
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