Open vkolova opened 1 year ago
same
This issue is very well known. Unfortunately, I don't understand the main reason behind the problem. Using Story()
instead of <Story />
is indeed our recommendation for everyone, who encounters this error. @shilman I wonder whether we should adjust the documentation to always use the Story()
syntax since the JSX one is coupled to this annoying "hooks can only be called inside decorators" issue.
I think that makes the most sense, @valentinpalkovic as it also makes the pattern consistent across all view layers.
I have the same issue with withThemeByDataAttribute
from @storybook/addon-themes
.
Hey @Arizona-dev, were you able to solve it by using the story as a function instead of JSX?
I was having this same issue, but tried swapping the order of decorators and that appeared to help
(Story) => {
return <div>{<Story/>}</div>;
},
withThemeByClassName({
themes: {
light: 'my-theme--light',
dark: 'my-theme--dark',
},
defaultTheme: 'light',
})
@Erns, can you try changing your custom decorator to return <div>{Story()}</div>
instead of using Story
as a JSX tag?
@Erns, can you try changing your custom decorator to return
<div>{Story()}</div>
instead of usingStory
as a JSX tag?
It does not work when working with RSC.
Here's a simple, minimal reproduction for this issue with Addon themes. The funny thing is that it does not happen with React Vite, but it happens with React + Nextjs.
https://github.com/yannbf/themes-hooks-issue-repro
If useCustomHook
is called inside of the render function, it breaks. If it's called as part of <Component />
, then it works.
const useCustomHook = () => {
const [value, setValue] = useState<number>()
useEffect(() => {
setValue(123)
}, [])
return value
}
const Component = () => {
const value = useCustomHook()
return <div>Hello world {value}</div>
}
export const Primary: Story = {
render: () => {
// if you uncomment the line below, the story will break with:
// Error: Storybook preview hooks can only be called inside decorators and story functions.
// useCustomHook()
return <Component />
},
}
@valentinpalkovic any idea? could it be related to SWC for some reason, given that it won't happen in React Vite?
cc @italoteix
I have a similar error, all my stories are okay but when I try to call a async component it breaks, but if I remove 'withThemeByClassName' works fine.
Minimal reproduction
import type { Meta, StoryObj } from '@storybook/react'
async function TestAsyncComponentExample(){
return <div> Holis</div>
}
const meta: Meta<typeof TestAsyncComponentExample> = {
title: 'Components/PressureLoadsPage',
component: TestAsyncComponentExample,
}
export default meta
type Story = StoryObj<typeof TestAsyncComponentExample>;
export const Default: Story = {
args: {},
}
I have a similar error, all my stories are okay but when I try to call a async component it breaks, but if I remove 'withThemeByClassName' works fine.
Minimal reproduction
import type { Meta, StoryObj } from '@storybook/react' async function TestAsyncComponentExample(){ return <div> Holis</div> } const meta: Meta<typeof TestAsyncComponentExample> = { title: 'Components/PressureLoadsPage', component: TestAsyncComponentExample, } export default meta type Story = StoryObj<typeof TestAsyncComponentExample>; export const Default: Story = { args: {}, }
Same issue with "async Component"
@shilman or @ShaunEvening can anyone boost this up? With RSC becoming a very common way to build NextJS apps now, this bug is starting to drive design decisions, when none of the suggested temp solutions seem to work. (Changing all decorators to be a function call, or moving withThemeByClassName
to the end of the list of global decorators in preview.tsx
). Having to make a choice to either not use RSC, or not use Storybook for any RSC components if we want theming support.. 😔
https://github.com/storybookjs/storybook/issues/24625 https://github.com/storybookjs/storybook/issues/26239 https://github.com/storybookjs/storybook/issues/22132 https://github.com/storybookjs/storybook/pull/26243
@chris-erickson can you do me a favor and try out our next vite based next framework
https://storybook.js.org/docs/get-started/frameworks/nextjs#with-vite
According to @yannbf 's comment above theming works in Vite. And while the new package is currently experimental it is way faster than the webpack framework and also compatible with all the next-gen testing stuff we're working on.
If it works it would be one more reason for us to elevate that to our recommended framework for Next.
Describe the bug
I get
Storybook preview hooks can only be called inside decorators and story functions.
error when combining@storybook/addon-themes
withThemeByClassName
when globally wrapping stories in markup.It's a React Vite (TypeScript) project.
To Reproduce
Any component that will re-render on interaction works.
This combination works:
But when
Story
is used as a component, as also show in the documation and how we've used it till we migrated to Storybook 7, it crashes:System
Additional context
I actually couldn't create a reproduction, because of error when transforming preview.ts (which probably indicates we shouldn't use JSX syntax?) We are using Babel in our project and everything works, up untill a component needs re-rendering. Am I missing something or is this expected behaviour?