Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React hooks documentation generator

What is the best way to generate documentation for React hooks in .jsx and .tsx files?

I tried TypeDoc, but I do not get any comments from function methods.

const MainFunction = () => {

  /**
   * How to get generate this comment?
   * 
   * @param a first number
   * @param b second number
   * 
   * @returns sum of and b
   */
  const sumNumbers = (a, b) => {
    return a + b;
  }
}
like image 548
john_per Avatar asked Feb 09 '26 23:02

john_per


1 Answers

Can't comment @OPearl question so I'll answer here, as it took me a while to figure it out and this seems to be the most pertinent post on the subject.

You can comment methods in functional component with better-docs but there are some limitations not quite clear from the docs.

@component

First of all the component needs to be tagged with the @component, which requires enabling the correspondent plugin, see docs. Tagging it with @component is going to give it a constructor status that lets you comment on methods and use other related tags like @memberOf

In order for the component to be displayed properly you need to export it separately after, so this is not going to work:

// Not working!
export default function Component()

Hooks

With the @component tag working you should be able to tag methods inside the component but it won't work with hooks. What you can do however is tag methods inside hooks by adding a @memberOf tag.

Example of working tagged methods in a functional component:

/**
 * Component comment
 *
 * @component
 */
function ExampleComponent(props) {
  /**
   * Method comment
   *
   * @param {string} foo - Foo description
   */
  function testFunction(foo) {
    // do something
  }

  useEffect(() => {
    /**
     * Method comment
     *
     * @param {number} bar - Bar description
     * @memberOf ExampleComponent
     */
    function testEffect(bar) {
      // do something
    }
    testEffect(1);
  }, []);

  return <div>foobar</div>;
}

export default ExampleComponent;
like image 77
gopicci Avatar answered Feb 12 '26 16:02

gopicci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!