Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using React.memo causes issues with Storybook Info Addon #6298

Closed
JKirkYuan opened this issue Mar 26, 2019 · 10 comments
Closed

Using React.memo causes issues with Storybook Info Addon #6298

JKirkYuan opened this issue Mar 26, 2019 · 10 comments

Comments

@JKirkYuan
Copy link

JKirkYuan commented Mar 26, 2019

Describe the bug
Wrapping a functional component with React.memo causes the Info Addon to display the Story Source as <Unknown /> and causes all the Prop Types to disappear

To Reproduce
Steps to reproduce the behavior:

  1. Wrap your functional component with React.memo
  2. Click on 'showInfo'
  3. We can see that the JSX is wrong and has no Prop Types

Screenshots

  • With React.memo
    ReactMemo
  • Without React.memo
    ReactWithoutMemo

Code snippets

webpack.config.js

const path = require('path');
module.exports = ({ config, mode }) => {
  config.module.rules.push({
    test: /\.(ts|tsx)$/,
    use: [
      {
        loader: require.resolve('babel-loader'),
	options: {
	  presets: [require.resolve('babel-preset-react-app')]
	}
      },
      {
        loader: require.resolve('react-docgen-typescript-loader'),
      },
    ]
  });
  config.resolve.extensions.push('.ts', '.tsx');
  return config;
};

story

storiesOf("InputField", module)
  .addParameters({
    info: {
      inline: false,
    }
  })
  .add(
    "Search", () => (
      <FieldDay onChange={action('change')}/>
    )
  );

System:

  • OS: Windows10
  • Browser: Chrome
  • Framework: React 16.8.4 (TypeScript)
  • Addons: addon-info
  • Version: 5.0.5 & 5.1.0-alpha.17
@shilman
Copy link
Member

shilman commented Apr 11, 2019

Ermahgerd!! I just released https://github.com/storybooks/storybook/releases/tag/v5.1.0-alpha.25 containing PR #6347 that references this issue. Upgrade today to try it out!

Because it's a pre-release you can find it on the @next NPM tag.

Closing this issue. Please re-open if you think there's still more to do.

@shilman shilman closed this as completed Apr 11, 2019
@kzinevych
Copy link

@shilman Hi, thx for the updates. In @next version (5.1.0-rc.3) using react memo, have same story with memo components (in JSX tab). <[object Object] /> instead component name <Modal />

@shilman
Copy link
Member

shilman commented May 31, 2019

@ConstRep Can you file an issue on addon-jsx repo for that? I don't maintain that package.

@ButuzGOL
Copy link

ButuzGOL commented Jun 6, 2019

in ^5.1.1 have
still issue

// @flow

import React, { memo } from 'react';
import classnames from 'classnames';

type Props = {
  /**
   * Type default, shade, inverse
   */
  type?: 'default' | 'shade' | 'inverse',
  /**
   * Label text
   */
  label?: string,
  /**
   * Class name
   */
  className?: string,
  /**
   * Childern nodes
   */
  children?: any,
};

const Badge = memo<Props>(({ children, type, className, label }: Props) => {
  const typeClassName = type ? `slds-theme_${type}` : null;
  const classNames = classnames('slds-badge', typeClassName, className);

  return <span className={classNames}>{label || children}</span>;
});

Badge.displayName = 'Badge';

export default Badge;

@shilman
Copy link
Member

shilman commented Jun 6, 2019

FYI this should be fixed in SB docs, which displays the actual source code. See the 5.1 release post for more info on how to kick the tires: https://medium.com/storybookjs/storybook-5-1-50afd02b5bbb

@ButuzGOL
Copy link

i have checked in 5.2.0-beta.38 still have issue
Prop Types
"Unknown" Component

const Badge = memo<Props>(({ children, type, className, label }: Props) => {
  const typeClassName = type ? `slds-theme_${type}` : null;
  const classNames = classnames('slds-badge', typeClassName, className);

  return <span className={classNames}>{label || children}</span>;
});

Badge.displayName = 'Badge';

export default Badge;

@shilman
Copy link
Member

shilman commented Aug 22, 2019

DocsPage is replacing addon-info. Announcement here: https://medium.com/storybookjs/storybook-docspage-e185bc3622bf

@Lighttree
Copy link

Still an issue in

"@storybook/react": "^5.2.3",
"@storybook/addon-docs": "^5.3.0-alpha.15"

As soon as I wrap my component in React.memo I start to have:
"No props found for this component" in <Props />

@shilman
Copy link
Member

shilman commented Oct 10, 2019

Will fix: #8361

@Chill-Studio
Copy link

Yup still happening in the last version 6.3.6

My solution is to export a version of the component without the memo and one with the memo and use the one without in storybook

const Component =  (p :  BlablaProps) : JSX.Element => {
    return (
       <div>Blabla</div>
    );
})
export const BlablaStoryBook  = Component
export const Blabla = memo(Component)

In the story

import { ComponentMeta, ComponentStory } from '@storybook/react';
import { BlablaStoryBook } from './';

export default {
  title: 'SomeSection/Blabla',
  component: BlablaStoryBook,
  argTypes: {
  
  },
} as ComponentMeta<typeof BlablaStoryBook>;

const Template: ComponentStory<typeof BlablaStoryBook> = (args) => <BlablaStoryBook {...args} />;

export const Primary = Template.bind({});
Primary.args = {

};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants