Classes and styles not aplied after Gatsby Build
See original GitHub issueI have been experiencing some unexpected behavior when trying to use styled-components
in a Gatsby build that only seems to happen in production builds. I have a hook that changes the value of props being passed to a styled component. When the page is loaded in the browser it appears as though the styles are correct in react dev tools but the DOM does not reflect these changes.
It works as intended with npm start
, but not with npm build
Example CodeSandbox: https://codesandbox.io/s/styled-components-issue-2629-js89j
Example Repo: https://github.com/BraveLilToaster/styled-components-issue-2629
Environment
System:
- OS: Linux 4.15 Ubuntu 18.04.1 LTS (Bionic Beaver)
- CPU: (8) x64 Intel® Core™ i7-8550U CPU @ 1.80GHz
- Memory: 5.31 GB / 15.12 GB
- Container: Yes
- Shell: 5.4.2 - /usr/bin/zsh
Binaries:
- Node: 10.16.0 - ~/.nvm/versions/node/v10.16.0/bin/node
- Yarn: 1.12.1 - /usr/bin/yarn
- npm: 6.9.0 - ~/.nvm/versions/node/v10.16.0/bin/npm
npmPackages:
- babel-plugin-styled-components: ^1.10.1 => 1.10.1
- styled-components: ^4.3.1 => 4.3.1
Reproduction
- Create new Gatsby project from the default-starter
npm install --save gatsby-plugin-styled-components styled-components babel-plugin-styled-components
- Add a styled component that takes a prop like color.
- Add a hook to set the color based on screen size.
Steps to reproduce
- Run
npm run build
- Run
npm run serve
Expected Behavior
Screen size is detected to be over 900px wide. Value of props passed down to the styled components change. Classes/styles update in the DOM.
Actual Behavior
Screen size is detected to be over 900px wide. Value of props passed down to the styled components. Classes/styles update in react dev tools, but the DOM maintains the same classes/styles from the initial load.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:24 (6 by maintainers)
Top GitHub Comments
I think I’m still encountering this as of v5. (if I understand the issue correctly 😳Gatsby, styled components, and hydration are pretty new turf for me)
My Gatsby site provides an item from localStorage to the styled-components ThemeProvider.
In dev mode, this is fine. The site loads in whatever mode was stored in localStorage. In production mode, however, upon loading the mode from localStorage, my
GlobalStyle
responds to the theme, whereas my styled components do not.Setting the theme through user interaction gets things going again. Setting the state with a useEffect() hook instead of with an initial value to useState also works; this is my current workaround, though it flashes a bit.
I tried to make a demo of this, both in a repo, and below in the Reproduction section.
Happy to answer questions or provide details!
Environment
package.json
gatsby-config.js
Reproduction
src/pages/index.js
Screenshots
Only in production, when
localstorage
is stored in localStorage, the whole site is supposed to turn green. However, only theGlobalStyle
responds:Toggling the theme back and forth, or setting it with useEffect instead of an initial value provided to useState, seems to fix the issue
@decepulis Thank you for the example repo! This was (I guess still is) driving me crazy.
I have nearly identical code to yours and exact same issues: My theme styles are loaded perfectly from localStorage in development mode. But when it comes to production, only some styles load and some do not (seems to be all the child components that do not load the theme styles). But if I were to change the theme through user interaction, voila! Everything works perfectly.
I currently am using your “useEffect” temporary solution. I suppose the “flash” is better than the mess of colors of multiple themes. I suppose we’re still not at a resolution to this issue?