storybook: [Bug]: Storybook preview hooks can only be called inside decorators and story functions.
Describe the bug
When attempting to use a custom render function to create a Story for a controlled component, I am getting the following error.
Storybook preview hooks can only be called inside decorators and story functions.
This previously worked fine, so I am curious what changed.
My Story code is as follows. I am just trying to use a basic implementation of useState. The Story renders correctly, but as soon as I press the Button, this error is displayed.
From what I can tell, this is being caused by @storybook/addon-themes. You can see my setup in the reproduction.
// .storybook/preview.ts
import type { Preview, ReactRenderer } from '@storybook/react';
import { withThemeByDataAttribute } from '@storybook/addon-themes';
const preview: Preview = {
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
decorators: [
withThemeByDataAttribute<ReactRenderer>({
themes: {
light: 'light',
dark: 'dark',
auto: 'auto',
},
defaultTheme: 'light',
attributeName: 'data-color-mode',
}),
],
};
export default preview;
type Story = StoryObj<typeof meta>;
export const Controlled: Story = {
args: {
size: 'small',
label: 'Button',
},
render: function Render(args) {
const [pressed, setPressed] = React.useState(false);
return (
<>
<Button {...args} onClick={() => setPressed(!pressed)} />
<h1>{pressed.toString()}</h1>
</>
);
},
};
Reproduction link
https://stackblitz.com/edit/github-rjewa6?file=.storybook%2Fpreview.ts
Reproduction steps
- Run the Storybook.
- Navigate to the Button -> Controlled Story
- Press the button
System
Storybook Environment Info:
System:
OS: macOS 15.0
CPU: (14) arm64 Apple M3 Max
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.16.0 - ~/.nvm/versions/node/v20.16.0/bin/node
npm: 10.8.1 - ~/.nvm/versions/node/v20.16.0/bin/npm
pnpm: 9.6.0 - ~/Library/pnpm/.tools/pnpm/9.6.0/bin/pnpm <----- active
Browsers:
Chrome: 129.0.6668.58
Safari: 18.0
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 39
- Comments: 30 (4 by maintainers)
Commits related to this issue
- chore: downgrade to stable version of storybook Ref: https://github.com/storybookjs/storybook/issues/29189 — committed to agrc/kitchen-sink by stdavis 2 years ago
- chore: downgrade to stable version of storybook Ref: https://github.com/storybookjs/storybook/issues/29189 — committed to agrc/kitchen-sink by stdavis 2 years ago
- chore: downgrade to stable version of storybook Ref: https://github.com/storybookjs/storybook/issues/29189 — committed to agrc/kitchen-sink by stdavis 2 years ago
Also facing the exact same issue, which appeared in version 8.3.0. For the moment, staying on 8.2.9 prevent the issue.
Working on a fix to finally solve this properly in Storybook 9!
@vanessayuenn I think we should put this as high priority in the empathy workstream.
Facing the same issue, it started happening with
8.3.0. It works fine till8.2.9. I have the pretty much the same story example as given in description.@yannbf This issue is really complicated and in fundamental way, this was broken for quite some time.
The issue seemed to have been aggravated by a RSC decorator change.
Here is a PR that should fix that issue: https://github.com/storybookjs/storybook/pull/26243
There will still be a problem when a user write a chain of decerator like this itself:
Or even a custom decorator that uses the storybook context in some way:
This is a very weird issue. We are not yet at the bottom of this problem.
We might be able to prevent it with a custom applyDecorator, in a similar way that Vue has:
https://github.com/storybookjs/storybook/blob/1fdd2d6c675b81269125af5027e45a357c09f1fa/code/renderers/vue3/src/decorateStory.ts#L47-L80
And add a (S) => decorator in between each other decorator in the stack. After some experimenting it seems that React doesn’t loose its react state in this way.
Had the same issue on my end, fixed it by using the useState from preview-api instead of React’s
Currently on storybook@8.4.7
version 8.5.0 also solved the issue for me 👍
Seems to be fixed in 8.5.0.
What is status here, @kasperpeulen? Have you gotten a solution/fix yet? I have the same issue in Angular.
I get the same thing with Storybook 9.0.6 using
@storybook/nextjs. However, I get it only inasyncstories withfeatures.experimentalRSC: truein.storybook/main.tsand:My guess is that there’s some kind of race condition between
useArgsinasyncRSC components. Perhaps the component takes too long, souseArgsof a previous render is called when a new render is already in process.This is still a problem. In my case I’m using the useDarkMode from storybook-dark-mode which I assume it must have an useState.
I’m on 8.5.1 and seeing this error. It’s intermittent which doesn’t make a lot of sense to me 😃. If I constantly refresh the page eventually the error goes away and my component renders. But that’s not a solution only an observation.
https://github.com/user-attachments/assets/b4252ebf-49fc-4295-995e-ae1daafdec3b
I was having the same issue in 8.4.7. Not using @storybook/addon-themes fixed it.
Simply adding “async” to my component broke it. This suggestion worked for me, but I had to add the <React.Suspense> wrapper.
Unfortunately, I still experience the same exception even after downgrading to the
8.2.10version!Here’s the story template I use:
Whenever I click on the button - the screen flashes with the exception:
https://github.com/user-attachments/assets/065934e8-ddf1-44c2-ba02-5491e5999ed5
The approach recommended by @damonaskavio helps, and after replacing the
useStateimports withimport { useState } from "storybook/internal/preview-api";- story renders successfully locally ✅. However, the same exception is thrown when the stories are being rendered within thejestruntime ❌UPD: The following recommendation helped me - https://stackoverflow.com/a/77859321/10963661. I moved the
withThemeFromJSXProviderdefinition to the last place in thedecoratorsarray. And then replaced hooks imports back with theimport { useState } from 'react'. After that - the stories finally started working again!A not so ideal work around is:
I love the
Empathy Backlogname. Sending warm vibes to Storybook comrades.Just notice this same issue today. Every story that has a useState is broken 😦
The same problem happens to me as well. Here is reproduction: chromatic preview, code