storybook: Cannot read property 'displayName' of undefined

Describe the bug

Test suite failed to run

    TypeError: Cannot read property 'displayName' of undefined

      1 | import initStoryshots from '@storybook/addon-storyshots'
      2 | 
    > 3 | initStoryshots({
        |               ^
      4 |   framework: 'react-native',
      5 | })
      6 | 

      at getDisplayName (node_modules/@emotion/primitives-core/dist/primitives-core.cjs.dev.js:232:64)
      at Function.createStyledComponent (node_modules/@emotion/primitives-core/dist/primitives-core.cjs.dev.js:225:41)
      at Object.<anonymous> (node_modules/@storybook/react-native/dist/preview/components/StoryListView/index.js:110:20)
      at Object.<anonymous> (node_modules/@storybook/react-native/dist/preview/components/OnDeviceUI/index.js:46:45)
      at Object.<anonymous> (node_modules/@storybook/react-native/dist/preview/index.js:58:42)
      at Object.<anonymous> (node_modules/@storybook/react-native/dist/index.js:12:39)
      at Object.load (node_modules/@storybook/addon-storyshots/dist/frameworks/rn/loader.js:36:29)
      at loadFramework (node_modules/@storybook/addon-storyshots/dist/frameworks/frameworkLoader.js:31:17)
      at Object.testStorySnapshots [as default] (node_modules/@storybook/addon-storyshots/dist/api/index.js:53:36)
      at Object.<anonymous> (__tests__/storyshots.spec.ts:3:15)

System:

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (3 by maintainers)

Most upvoted comments

In my case I got the error by importing {Component} instead of Componet without the curly braces

In my case it was because I imported the component as a named import instead of default import.

So I fixed it from this:

import { Button } from './Button';

To this:

import Button from './Button';

Check the components you are importing. If they are undefined then you will get this error.

Seems like the cache is messing up somehow. Removing the npm cache and restarting storybook solves things, but the problem comes back.

rm -rf ./node_modules/.cache
yarn storybook

@stereodenis I appear to be facing this same issue - how was it resolved for you?

I had the same issue, in my case it was indeed due to an imported module being undefined in one of my deeply nested components. The error is pretty cryptic and did not lead me to the actual bug at all. I was able to debug it by commenting and uncommenting code to check when it is working and when it is not.

For me, this was happening on the initial render, but if I changed anything in the corresponding component (which possibly lead to re-loading the incorrect module), it reloaded the UI due to HMR, and it worked fine. This was happening / breaking when I refreshed the page as well.

The problematic module was using some global variables, which were not defined in storybook webpack. Once that was fixed, this problem went away.

Still don’t know why it did not work initially, but worked on HMR (after making any random changes in the files, which lead to re-building the module once again i guess)

Reason For Comment

My comment is quite late (2 years later), but I wanted to share what I found recently, which fixed a problem with the displayName error in a Storybook.

My Scenario

In my scenario I had a hoisted constant inside the .stories.tsx file which was referenced as test data for the Story render function.

In my case, the component had a complex type which cannot be used as an argument for the story - list of ReactNode.

Example

Here is a simple example of how to replicate this bug and how to “fix” it.

// FruitList.tsx
import { FC, ReactNode } from 'react';

interface Fruit {
  name: string;
  color: string;
  icon: ReactNode;
}

interface FruitsListProps {
  fruits: Fruit[];
}

export const FruitsList: FC<FruitsListProps> = ({ fruits }) => {
  return (
    <ul>
      {fruits.map((fruit, index) => (
        <li key={index} style={{ color: fruit.color, display: 'flex', alignItems: 'center', gap: 8 }}>
          {fruit.name}

          {fruit.icon}
        </li>
      ))}
    </ul>
  );
};


Hoisted Constant Inside Storybook

// FruitList.stories.tsx - Hoisted FRUITS_LIST constant

import type { Meta, StoryObj } from '@storybook/react';

import { FruitsList } from './FruitsList';

const FRUITS_LIST = [
  {
    name: 'Strawberries',
    color: '#C70039',
    icon: (
      <img src="https://www.publicdomainpictures.net/pictures/450000/velka/strawberries-vintage-art.png" width={30} />
    ),
  },
  {
    name: 'Cherries',
    color: '#900C3F',
    icon: (
      <img
        src="https://www.publicdomainpictures.net/pictures/360000/velka/kirschen-obst-frucht-vintage.png"
        width={30}
      />
    ),
  },
  {
    name: 'Bananas',
    color: '#FFD700',
    icon: (
      <img src="https://www.publicdomainpictures.net/pictures/440000/velka/banana-fruit-vintage-art.png" width={30} />
    ),
  },
];

export default {
  title: 'Components/FruitsList',
  component: FruitsList,
  tags: ['autodocs'],
  argTypes: {
    fruits: {
      control: {
        disable: true,
      },
    },
  },
  render: (props) => <FruitsList {...props} fruits={FRUITS_LIST} />,
} as Meta<typeof FruitsList>;

type Story = StoryObj<typeof FruitsList>;

export const Default: Story = {};

Image

Inline variable

// FruitList.stories.tsx - inlined fruits list
import type { Meta, StoryObj } from '@storybook/react';

import { FruitsList } from './FruitsList';

export default {
  title: 'Components/FruitsList',
  component: FruitsList,
  tags: ['autodocs'],
  argTypes: {
    fruits: {
      control: {
        disable: true,
      },
    },
  },
  render: (props) => (
    <FruitsList
      {...props}
      fruits={[
        {
          name: 'Strawberries',
          color: '#C70039',
          icon: (
            <img
              src="https://www.publicdomainpictures.net/pictures/450000/velka/strawberries-vintage-art.png"
              width={30}
            />
          ),
        },
        {
          name: 'Cherries',
          color: '#900C3F',
          icon: (
            <img
              src="https://www.publicdomainpictures.net/pictures/360000/velka/kirschen-obst-frucht-vintage.png"
              width={30}
            />
          ),
        },
        {
          name: 'Bananas',
          color: '#FFD700',
          icon: (
            <img
              src="https://www.publicdomainpictures.net/pictures/440000/velka/banana-fruit-vintage-art.png"
              width={30}
            />
          ),
        },
      ]}
    />
  ),
} as Meta<typeof FruitsList>;

type Story = StoryObj<typeof FruitsList>;

export const Default: Story = {};

Image

Possible Explanation

It seems that Storybook is indexing the *.stories.tsx files and cannot access any JSX defined in the same file outside the Story definition closure. Take it with a grain of salt as I do not have concrete proof why this is happening.

Remove your child components one by one, as this will help you identify which one is missing a property.