storybook: Addon-docs: 'show code' not displaying correct component name when using forwardRef with args
Describe the bug
When using args with a component that uses forwardRef, you will not be shown the correct component name when clicking ‘Show code’ in the docs section of storybook
To Reproduce Repo: https://github.com/Jamess-Lucass/storybook-issue-reproduction
- Create a component using
forwardRef - Use that component with the args feature within storybook
System
Environment Info:
System:
OS: Linux 5.10 Ubuntu 20.04.4 LTS (Focal Fossa)
CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
Binaries:
Node: 16.16.0 - ~/.nvm/versions/node/v16.16.0/bin/node
npm: 8.11.0 - ~/.nvm/versions/node/v16.16.0/bin/npm
npmPackages:
@storybook/addon-actions: 6.5.10 => 6.5.10
@storybook/addon-essentials: 6.5.10 => 6.5.10
@storybook/addon-interactions: 6.5.10 => 6.5.10
@storybook/addon-links: 6.5.10 => 6.5.10
@storybook/builder-vite: 0.2.2 => 0.2.2
@storybook/react: 6.5.10 => 6.5.10
@storybook/testing-library: 0.0.13 => 0.0.13
Additional context
I am experiencing this issue when the component uses forwardRef and your story is using args. The below code will replicate this error.
currently on version 6.5.10
button.tsx - normal component
import { forwardRef } from "react";
export type ButtonProps = {
text: string;
};
const Button = forwardRef<HTMLButtonElement, ButtonProps>(
({ text }: ButtonProps, ref) => <button ref={ref}>{text}</button>
);
// const Button = ({ text }: ButtonProps) => <button>{text}</button>;
export default Button;
button.stories.tsx - story file
import { ComponentMeta, ComponentStory } from "@storybook/react";
import Button from "./button";
export default {
title: "Button",
component: Button,
} as ComponentMeta<typeof Button>;
const Template: ComponentStory<typeof Button> = ({ text }) => (
<Button text={text} />
);
// const Template: ComponentStory<typeof Button> = () => (
// <Button text="button text" />
// );
export const Default = Template.bind({});
Default.args = {
text: "Best button!",
};
When viewing the docs in storybook you will see the component loaded as _c:

If you remove args in the button.stories.tsx file it will work:
const Template: ComponentStory<typeof Button> = () => <Button text="test" />;
This is not feasible for me as I want to use the args feature.
Likewise, if you were to not use forwardRef in the button.tsx component it will work with args:
const Button = ({ text }: ButtonProps) => <button>{text}</button>;
This is also not feasible for me as I want to use forwardRef in the component and others may not have direct access to the component if they’re using a library like MUI or Chakra
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 3
- Comments: 15 (7 by maintainers)
Instead of creating a new component wrapper, you can set the
displayNameof the component:Component.displayName = 'Component';. Unfortunately, I haven’t found a config option to prevent this name replacement.@warman-suganda’s suggestion worked for me, but only in dev mode 😢
I have the same problem.
@Jamess-Lucass
Yes, you’re right, the difference between the 2 name conventions is if triggered
isForwardRefcondition, I am not sure if it’s a bug 🐛.