Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSDoc Tag Support for React HOC (Higher-Order Component)

Tags:

What is the proper way to document a React Higher-Order Component using JSDoc? There aren't, of course, any React-specific tags built-in - so what would be the proper approach?

like image 508
anil kumar Reddy Avatar asked May 06 '17 04:05

anil kumar Reddy


People also ask

Can higher-order components be used with functional components?

Higher-Order Components enable us to apply functional programming principles on components by embracing composition. React Hooks, in contrast, transformed pure (in the sense of functional programming) function components to stateful/side-effect burdened beasts.

What is a higher-order component HOC )?

A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per se. They are a pattern that emerges from React's compositional nature. Concretely, a higher-order component is a function that takes a component and returns a new component.

How would you create higher-order components HOCs in React?

We can invoke the HOC as follows: const SimpleHOC = higherOrderComponent(MyComponent); A higher-order component transforms a component into another component. Note that a HOC doesn't modify the input component.

What is a higher-order component give us an example?

Higher-Order Components are not part of the React API. They are the pattern that emerges from React's compositional nature. The component transforms props into UI, and a higher-order component converts a component into another component. The examples of HOCs are Redux's connect and Relay's createContainer.


1 Answers

To document a high-order component with JSDoc, you would follow the standard as laid out below. To clarify, the documentation is usually found where the higher-order component is defined and not necessarily where it's used. If you want to document where it's used, that can be documented in that component's JSDocs as a @description, @see, @link, or other relevant tags.

  import React, { useState } from 'react';
  import Async from 'react-async';
  import { withTheme } from 'design'; // Contains the JSDoc for this HOC.
  import { getName } from 'api';

  /**
    * Component to render a greeting to someone!
    * 
    * @component
    * @param {string} userId The ID of the user to greet
    */
  export function Hello({ theme, userId }) {
    return (
      <Async promiseFn={getName} userId={userId}>
        <Async.Fulfilled>
          {(data) => <p>Hello {data.name}!</p>}
        </Async.Fulfilled>
      </Async>
    );
  };

  export default withTheme(Hello);

Documentation with React, and code in general, is always interesting. There are several ways and along with it, advantages and disadvantages of each. Ask yourself what it's going to be used for? Creating a documentation website, sharing between teammates, developers only, etc.? That being said, the most common ways to purely document are JSDoc and Markdown.

  • Markdown (e.g., Material UI)
  • MarkdownX (MDX

There are other ways to visualize components that have built-in documentation functionality usually based around Markdown or JSDoc as well. This becomes extremely useful when working within a team or publishing packages.

  • Bit (Bit Documentation)
  • Storybook (Storybook for MD)
  • React StyleGuidist (StyleGuidist Docs)
like image 143
Ross Sheppard Avatar answered Nov 04 '22 22:11

Ross Sheppard