react-docgen icon indicating copy to clipboard operation
react-docgen copied to clipboard

Can a React component returns null?

Open marexandre opened this issue 4 years ago • 2 comments

Have some custom components wrapping 3rd party lib that has nothing to do with React. Is it possible to get props when a React components returns null?

React component code looks something like this:

import coolLib from 'cool-lib';

export default function MyComponent({ id }) {
  useEffect(() => {
    const lib = new coolLib({ id });

    lib.on('load', () => {
      // do stuff
    });

    return () => {
      coolLib.remove()
    }
  }, []);

  return null;
}

MyComponent.propTypes = {
  id: PropTypes.string.isRequired
}

If I return <div>{id}</div> or some other JSX then seeing props getting pulled out, but not when return null;, is this use case not supported in react-docgen?

marexandre avatar Aug 20 '21 16:08 marexandre

yes it can, did that multiple times. Please rewrite your export default to use memo(MyComponent) for perf

JustFly1984 avatar Sep 27 '21 13:09 JustFly1984

I've originally asked this Q in Storybook js repo and was suggested to ask here: https://github.com/storybookjs/storybook/discussions/15857#discussioncomment-1210046 Returning null skips generating argsTable is storybook, so yes a component can return null but I want to know by retuning null does react-docgen skip getting props for that component or not...

marexandre avatar Sep 27 '21 15:09 marexandre

In version 6 it will be possible to use a comment to mark a function as a component with @component.

danez avatar Apr 01 '23 18:04 danez