storybook: [Bug]: Show code displays `<[object Object]...` instead of component name

Describe the bug

Components that are wrapped with forwardRef in react are not displaying the proper name in built Storybook, whole working as expected in development mode. Example of what I mean in the image below.

image

Tried to debug by modifying and logging what is passed here: https://github.com/storybookjs/storybook/blob/next/code/renderers/react/src/docs/jsxDecorator.tsx#L108

image

and indeed the element is different in dev vs prod.

After digging with the help of @IanVS and @JReinhold found out that this might be an issue with react-docgen-typescript and tsconfig.json of the project.

To Reproduce

Visit this link: https://github.com/konsalex/design-system-newline-course/tree/storybook-v7-issue
Clone then:


// Install packages
yarn
yarn workspace @newline-ds/foundation build
yarn workspace @newline-ds/react build

// Build and run
yarn workspace @newline-ds/storybook build-storybook && live-server packages/storybook/storybook-public


### System

```shell
Environment Info:

  System:
    OS: macOS 13.0
    CPU: (10) x64 Apple M1 Max
  Binaries:
    Node: 16.19.0 - ~/Library/Caches/fnm_multishells/44085_1675428690209/bin/node
    Yarn: 3.2.1 - /usr/local/bin/yarn
    npm: 8.19.3 - ~/Library/Caches/fnm_multishells/44085_1675428690209/bin/npm
  Browsers:
    Chrome: 109.0.5414.119
    Firefox: 109.0
    Safari: 16.1

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 6
  • Comments: 24 (10 by maintainers)

Most upvoted comments

Hey everyone! This issue is solved in #26566 and will be released soon. Thanks for your patience 🙏

Can confirm this is happening on forwarded refs with .displayName set after upgrading to Storybook 8.

Adding displayName to the component showed me the correct thing in the storybook for me

Button.displayName = "Button"

Seems like .displayName is now becoming the cause of <[object Object... in v8.

I have the same problem after migrating to 8. Here is a simple example from storybook.

const meta: Meta<typeof Button> = {
  component: Button,
  //👇 Enables auto-generated documentation for the component story
  tags: ['autodocs'],
};

export default meta;
type Story = StoryObj<typeof Button>;

export const Primary: Story = {
  args: {
    variant: "default",
  },
};

Here it is the auto docs output inside the code section. <[object Object] variant="default" />

Hey everyone! This issue is solved in #26566 and will be released soon. Thanks for your patience 🙏

In all stories with arguments for controls, as in the example, we still have the problem after the update.

Screenshot 2024-03-19 alle 16 50 53 Screenshot 2024-03-19 alle 16 53 03

It’s indeed the displayName that is causing the issue. Removing it works fine but there is obviously a bug or does the V8 implement it with custom Storybook logic based on the imported component name ? I’ve seen no motion of that in the migration guide.

It should not be the case because we must keep the posibility to add the displayName for devtools DOM support for example in some cases.

Indeed, having the same issue after upgrading to Storybook 8 from rc.3. Both with and without reactDocgen: "react-docgen-typescript", using webpack

Replicated with default npx create-next-app@latest and npx storybook@latest init

tested with and without (because docgen still doesnt support forwardRef) : main.ts

  typescript: {
    reactDocgen: "react-docgen-typescript",
  }

Using Storybook Example Button.tsx

/**
 * Primary UI component for user interaction
 */
export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  (
    { primary = false, size = "medium", backgroundColor, label, ...props },
    ref
  ) => {
    const mode = primary
      ? "storybook-button--primary"
      : "storybook-button--secondary";
    return (
      <button
        ref={ref}
        type="button"
        className={["storybook-button", `storybook-button--${size}`, mode].join(
          " "
        )}
        {...props}
      >
        {label}
        <style jsx>{`
          button {
            background-color: ${backgroundColor};
          }
        `}</style>
      </button>
    );
  }
);

Button.displayName = "Button";

Hey @shilman

So things get a little weird… I setup three identical buttons components. The only differences are

NoForwardRefButton

export const NoForwardRefButton = function (
  { className, variant, size, asChild = false, ...props }: ButtonProps
) {/*...*/};

ForwardRefNoDisplayNameButton

export const ForwardRefNoDisplayNameButton = forwardRef(function (
  { className, variant, size, asChild = false, ...props }: ButtonProps,
  ref: ForwardedRef<HTMLButtonElement>,
) {/*...*/});

ForwardRefWithDisplayNameButton

export const ForwardRefWithDisplayNameButton = forwardRef(function (
  { className, variant, size, asChild = false, ...props }: ButtonProps,
  ref: ForwardedRef<HTMLButtonElement>,
) {/*...*/});

ForwardRefWithDisplayNameButton.displayName = 'MyForwardRefWithDisplayNameButton';

I then ran the build with the default options and then with react-docgen-typescript

react-docgen

NoForwardRefButton image ForwardRefNoDisplayNameButton image ForwardRefWithDisplayNameButton image

react-docgen-typescript

NoForwardRefButton image ForwardRefNoDisplayNameButton image ForwardRefWithDisplayNameButton image

I just did a stackblitz to check if it’s still the case in 8.1 alpha, and it is - https://stackblitz.com/edit/github-umceda?file=src%2Fstories%2FButton.tsx,.storybook%2Fmain.ts&preset=node