Can't access styled-components theme in stories
See original GitHub issueDescribe the bug
I’m using styled-components
in combination with @storybook/react
. I’m wrapping all my stories in the SC ThemeProvider
like this:
addDecorator((storyFn) => (
<ThemeProvider theme={themeBasic}>
{storyFn()}
</ThemeProvider>
));
This works fine and all of my Styled Components have access to the theme. However, I also want to access the theme in my stories:
import { ThemeContext } from 'styled-components';
export const Configurable = () => {
const theme = useContext(ThemeContext);
console.log(theme); // undefined
return (
// Some React stuff here
);
};
Shouldn’t this be working?
System:
Environment Info:
System:
OS: macOS 10.15
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Binaries:
Node: 10.16.0 - /usr/local/bin/node
Yarn: 1.17.3 - /usr/local/bin/yarn
npm: 6.12.0 - /usr/local/bin/npm
Browsers:
Chrome: 78.0.3904.70
Firefox: 69.0.3
Safari: 13.0.2
npmPackages:
@storybook/addon-actions: ^5.2.5 => 5.2.5
@storybook/addon-backgrounds: ^5.2.5 => 5.2.5
@storybook/addon-info: ^5.2.5 => 5.2.5
@storybook/addon-knobs: ^5.2.5 => 5.2.5
@storybook/react: ^5.2.5 => 5.2.5
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:26 (6 by maintainers)
Top Results From Across the Web
Using Styled-Components, why can't I access the theme from ...
When I console.log props, the theme is there. The font-weight works, and it works fine if I substitute a hex value for the...
Read more >Creating Themes for React Styled Components - YouTube
This tutorial focuses primarily on creating and using styled - component Themes. The ThemeProvider and useTheme hook are both discussed and ...
Read more >Theming in React with Styled Components | by Ross Bulat
Styled Components aims to solve the problem of applying CSS to Javascript frameworks on a per-component basis; and this is particularly useful with...
Read more >How to add a theme switcher to Storybook - JS.ORG
Use a decorator to pass the theme object to your components · Switch the theme dynamically from the toolbar or using story parameters...
Read more >How to Refactor SCSS Variables for Styled Components
Styled component files are CSS-in-JS, so they are actually JavaScript files. Obviously, you can't write pure CSS in a JS file, so you...
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
I guess this issue is the same as https://github.com/storybookjs/storybook/issues/8426. If so, this could solve your problem. (NOTE: I didn’t test the code. Just saw the workaround.)
According to the latest docs (v5.3), you need to add a global decorator in
.storybook/preview.js
:https://storybook.js.org/docs/addons/introduction/#storybook-decorators