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

HOC as a function won't get generated docs

Open rsouthgate opened this issue 8 years ago • 9 comments

Similar to #177 but a slightly different scenario...

Given a HOC method like so:

import { bool } from 'prop-types';

const withSomeExtraProps = Wrapped => {
  class HigherOrderComponent extends Component  {
    static propTypes = Object.assign({}, Wrapped.propTypes, {
      /**
       * This should be picked up in the docgenInfo
       */
      anExtraProp: bool,
    });
    static defaultProps = Object.assign({}, Wrapped.defaultProps, {
      anExtraProp: false,
    });
    // ....the rest of the HOC logic
  }
  return HigherOrderComponent;
}

export default withSomeExtraProps;

I understand that this wouldn't automatically mean that the export default withSomeExtraProps(Wrapped) wouldn't automatically get the docgenInfo from the HOC added, but I thought individually this HOC would have it's own docgenInfo that I could reference on it's own.

However, it seems the inner component class here doesn't get recognised and parsed. Does the component class need to be exported for the parser to find it?

rsouthgate avatar Aug 14 '17 19:08 rsouthgate

Does the component class need to be exported for the parser to find it?

In the default configuration yes. However, we have multiple resolvers that you can use to e.g. find all components defined in a file.

fkling avatar Sep 11 '17 23:09 fkling

I am running into the same situation. I've tried using the findAllComponentDefinitions.js resolver but it it still failing to identify the properties of the HOC. @rsouthgate Were you successful using an alternate resolver?

purdrew avatar Oct 02 '17 18:10 purdrew

@deedubbu, you could also try https://github.com/Jmeyering/react-docgen-annotation-resolver .

fkling avatar Oct 10 '17 22:10 fkling

Been having troubles with HOC as well. Boiling it down to the minimum, consider:

import React from 'react';
const enhanced = () => () => <div />;
export default enhanced;

I get:

Error: No suitable component definition found.

Are functions that return components (HOCs) not supported at all? Tried all the resolvers without success.

Though the end goal would be to pick up the propTypes definitions from the inner component as well.

joaovieira avatar Oct 11 '17 15:10 joaovieira

Are functions that return components (HOCs) not supported at all?

They are not, at least not by default. The problem is that there are too many ways to define HOCs.

However, there is also nothing stopping anybody to write specific resolvers and handlers for specific HOCs 😉

fkling avatar Oct 11 '17 16:10 fkling

Cool. Will have a look at that. Thanks for the quick reply 👍

joaovieira avatar Oct 11 '17 16:10 joaovieira

I couldn't get the https://github.com/Jmeyering/react-docgen-annotation-resolver to work with my HOC which is a function as well. Would love some direction on how to go about writing a custom one. Thanks!

davidungio avatar Jun 15 '18 05:06 davidungio

We have some HOCs in our code base along with our functions, constants, and stuff. It would be nice if react-docgen expanded its remit beyond strictly components. Are there any tools out there that cover both components and non-component code? typedoc looks nice for TypeScript projects, but it doesn't handle React components very well. :(

k4b7 avatar Aug 10 '19 18:08 k4b7

My workaround for this issue:

const BaseComponent = withMyHOC(
  forwardRef(({ children, ...rest }, ref) => {
    return <div ref={ref}>{children}</div>;
  })
);

// added a comment explain this
const Component = props => <BaseComponent {...props} />;

kaiofelipejs avatar Feb 16 '23 13:02 kaiofelipejs