Cookies
This website uses cookies to improve the user experience, more information on the legal information path.
Notes
0
years
0
mons
0
days
0
hour
0
mins
0
secs
Sat16Mar8:23 PM
This is the background image
2

How document my react components with JSDoc

Mon, Jan 16, 2023

Introduction

It is necessary to have a good documentation about our components, so that other developers can understand how it works and how to use it. In this article we are going to see how we can document our react components with JSDoc and how to publish the documentation in our static site.

With a few simple steps, the jsdoc library and the use of good programming practices when working with react, we will be able to have a documentation of our components automatically. There are different plugins to upgrade our jsdoc and support react, but as React is still javascript and the components functions the default lib is more than enough.

The report we are going to generate is still an html file, so you can deploy it in any static site.

If you use typescript you will need to install jsdoc-babel

1. Installation


yarn add jsdoc-babel -D

2. Configuration

jsdoc.json

{
"tags": {
"allowUnknownTags": true,
"dictionaries": ["jsdoc", "closure"]
},
"source": {
"include": ["src/components/"],
"includePattern": ".+\\.tsx$",
"excludePattern": "(node_modules/|docs)"
},
"plugins": ["plugins/markdown", "node_modules/jsdoc-babel"],
"templates": {
"cleverLinks": false,
"monospaceLinks": false
},
"babel": {
"extensions": ["tsx"],
"ignore": ["**/*.(test|spec).ts"],
"babelrc": false,
"presets": [["@babel/preset-env", { "targets": { "node": true } }], "@babel/preset-typescript"],
"plugins": ["@babel/proposal-class-properties", "@babel/proposal-object-rest-spread"]
},
"opts": {
"encoding": "utf8",
"destination": "./public/docs/",
"recurse": true,
"verbose": true
}
}

3. Customise your report

custom-docs.js

import { readFile, writeFile } from 'fs';
import glob from 'glob';
glob('public/docs/**/*.?(html|css)', function (err, files) {
if (err) {
console.log('err', err);
return;
}
files.forEach((path) => {
readFile(path, 'utf8', (err, data) => {
...code
});
});
});

There you should find the code you need to modify your report and overwrite it.

4. Generate your report


yarn jsdoc -c jsdoc.json

To see an example of the output you can click above where it says Docs or via this link