Storybook not compatible with React hooks
See original GitHub issueDescribe the bug
Attempting to render a react component that uses hooks into a storybook staging environment throws an error Hooks can only be called inside the body of a function component.
To Reproduce Steps to reproduce the behavior:
- Create a react component that uses hooks
- Import & render the component in storybook
Expected behavior Storybook should be able to display React components that use hooks.
Code snippets
Component code
import React, { useState } from "react";
export default function ColorChanger() {
const [color, setColor] = useState("#000");
const randomColor = "#" + Math.floor(Math.random() * 16777215).toString(16);
return (
<div style={{ color }} onClick={() => setColor(randomColor)}>
Color is: {color} (click to change)
</div>
);
}
Note, this code is working on codesandbox: https://codesandbox.io/s/n5rmo77jx0
System:
- Browser: Firefox
- Framework: React
- Version: 4.0.2
Issue Analytics
- State:
- Created 5 years ago
- Reactions:25
- Comments:73 (25 by maintainers)
Top Results From Across the Web
React hooks can't work inside storybook for react native
I'm facing the following react hook error in my Storybook. Error: Invalid hook call. Hooks can only be called inside of the body...
Read more >Sharing React Hooks with Storybook - By Naomi Jacobs
This hook is pretty simple, but what if we had one more complicated? It's not immediately obvious what this hook returns, or how...
Read more >Documenting React Hooks with Storybook - Josh Farrant
Storybook is great for documenting UI components, but did you know it can document your shared React Hooks too?
Read more >Storybook — React Native, React, Angular, Vue | by KPITENG
You'll be prompted asking if you want to install @storybook/react-native-server, you can safely choose not to install this now since you can add...
Read more >Decorators - Storybook - JS.ORG
See https://storybook.js.org/docs/react/configure/overview#configure-story- ... these values using Storybook's UI. hooks - Storybook's API hooks (e.g., ...
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
Top Related Hashnode Post
No results found

Try:
in your .config file.
This should do the trick - for now.
So I ran into this issue using CRA 2.1.1 with typescript on the 4.1.0-alpha.8 version of
@storybook/reactand hooks and it ended up coming down to the fact that the package includes it’s own local copy of React so when the hook is called it can’t find theReactSharedInternals.ReactCurrentOwner.currentDispatcherinstance. As silly as it felt, I just updated my npm scripts to the following:And everything seemed to work. Maybe react/react-dom need to be peer-dependencies of
@storybook/reactinstead of regular deps?I tried the
react-hot-loadersetConfig thing only to find that react-hot-loader wasn’t installed in the first place. Maybe this is a change with the 4.1 alpha but I’m new to storybook in general so I wouldn’t know.