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:
- Created 3 years ago
- Reactions:1
- Comments:23
Top 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 >
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 Free
Top 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

@toomuchdesign I was just trying to make this work locally and I failed. I think its clear why the
themeis undefined in āserverā context ifstyled-componentsis not listed innonIsolatedModulesoption.However if itās listed in
nonIsolatedModulesthere will be a runtime error during SSR rendering. The problem is what if we list a module insidenonIsolatedModulesit 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-componentsat 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: booleanoption. If that option is disabled:We wouldnāt do the server render + hydrate, but just do the client side rendering. I quickly tried that locally and the
styled-componentsexample seemed to work.@ThePicoNerd I have a feeling you need to patch-jest; or have you done that already?