question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Storybook not compatible with React hooks

See original GitHub issue

Describe 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:

  1. Create a react component that uses hooks
  2. 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:closed
  • Created 5 years ago
  • Reactions:25
  • Comments:73 (25 by maintainers)

github_iconTop GitHub Comments

17reactions
phaistoniancommented, Nov 3, 2018

Try:

import { setConfig } from 'react-hot-loader';
setConfig({ pureSFC: true });

in your .config file.

This should do the trick - for now.

16reactions
Syynthcommented, Nov 28, 2018

So I ran into this issue using CRA 2.1.1 with typescript on the 4.1.0-alpha.8 version of @storybook/react and 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 the ReactSharedInternals.ReactCurrentOwner.currentDispatcher instance. As silly as it felt, I just updated my npm scripts to the following:

"storybook": "rm -rf ./node_modules/@storybook/react/node_modules/react && rm -rf ./node_modules/@storybook/react/node_modules/react-dom && start-storybook -p 9009",

And everything seemed to work. Maybe react/react-dom need to be peer-dependencies of @storybook/react instead of regular deps?

I tried the react-hot-loader setConfig 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found