storybook: Addon-docs: forwardRef trips up Props block
Describe the bug When implementing a Component in the way described below, the Props block only shows the following text:
“Cannot read property ‘appearance’ of undefined”
To Reproduce Declare a component in way mentioned under “Code snippets”.
Expected behavior The Props block should display the corresponding Props.
Screenshots
Code snippets Button.tsx
type ButtonProps = {
appearance?: "primary" | "secondary" | "ghost";
} & JSX.IntrinsicElements["button"];
const Button: RefForwardingComponent<HTMLButtonElement, ButtonProps> = React.forwardRef(({ appearance, ...otherProps}, ref) => (
<button>...</button>
))
Button.displayName = "Button";
Button.defaultProps = {
appearance: "primary"
};
Button.stories.tsx
import React from "react";
import { storiesOf } from "@storybook/react";
import { Button } from "./Button";
storiesOf("Button", module)
.addParameters({
component: Button
})
.add("primary", () => (
<Button appearance="primary">
My primary button!
</Button>
))
;
System: Tried out with both Storybook 5.2.5 and Storybook 5.3.0-beta.1, using react-docgen-typescript-loader 3.6.0.
Using stories in the TSX file format but encountered the error also when using the JSX file format.
Component is in TSX format.
Please paste the results of npx -p @storybook/cli@next sb info
here.
System:
OS: macOS 10.15.1
CPU: (4) x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
Binaries:
Node: 10.16.0 - ~/.nvm/versions/node/v10.16.0/bin/node
npm: 6.9.0 - ~/.nvm/versions/node/v10.16.0/bin/npm
Browsers:
Chrome: 78.0.3904.97
Safari: 13.0.3
npmPackages:
@storybook/cli: ^5.3.0-beta.1 => 5.3.0-beta.1
Additional context
Props block used to work before introducing RefForwardingComponent
and React.forwardRef
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 11
- Comments: 21 (3 by maintainers)
If it helps anyone coming from Google there was a subtle distinction that was needed for my case:
Storybook version:
6.4.20
I was already destructuring
forwardRef
upon import (as many people in various Github issues have suggested), explicitly setting the types upon invokingforwardRef<A, B>()
, and ensuring my component function was set to aconst
:However the key appears to be that you need to do the invocation of
forwardRef
within theconst
and not uponexport default
, like I did above. Here’s the working code:Hope this saves someone some time 😃
I had the same problem with the
6.1.20
version. It turned out you should not useReact.xxx
reference and directly use the decomposed memberxxx
:Moreover, just changing the type while Storybook is running in watch mode does not work. You need to restart the storybook (or force a full recompilation of the file).
@RodrigoTomeES
I had the exact same situation where I had a polymorphic component that renders a different element type based on an
as
prop and uses a generic to impose type safety.I redeclared the
forwardRef
definition using the approach found here and controls work just fine in storybook now.My final solution looked like this:
Got it working 👌
The only solution that worked for me is
Supplying a type to forwardRef such as
forwardRef<HTMLDivElement>
broke. With the ref type added, it seems to work. It also doesn’t like exportdefault forwardRef(Foo)
as others have stated.Removing
react-docgen-typescript-loader
and addingbabel-plugin-react-docgen
worked for me, as documented here: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#react-prop-tables-with-typescript@Zunaib this was fixed for me in Storybook 5.3
Mirror issue on react-docgen-typescript-loader side: https://github.com/strothj/react-docgen-typescript-loader/issues/76