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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With