Convert Figma logo to code with AI

reactjs logoreact-docgen

A CLI and library to extract information from React component files for documentation generation purposes.

3,824
314
3,824
101

Top Related Projects

246,614

The library for web and native user interfaces.

Runtime type checking for React props and similar objects

A simple parser for react properties defined in typescript instead of propTypes.

90,409

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation

15,447

An API documentation generator for JavaScript.

Quick Overview

React-docgen is a CLI and toolbox to extract information from React component files for documentation generation purposes. It analyzes the source code of React components and extracts metadata about their props, methods, and other relevant information, making it easier to generate comprehensive documentation for React projects.

Pros

  • Automates the process of extracting component documentation
  • Supports various React component definition styles (class, functional, etc.)
  • Integrates well with other documentation generation tools
  • Customizable through plugins and custom handlers

Cons

  • May struggle with complex or non-standard component structures
  • Limited support for some advanced TypeScript features
  • Requires manual configuration for optimal results in some cases
  • Documentation can become outdated if not regularly maintained

Code Examples

  1. Basic usage to extract component information:
import { parse } from 'react-docgen';

const componentCode = `
  import React from 'react';
  
  const MyComponent = ({ name }) => <div>Hello, {name}!</div>;
  
  MyComponent.propTypes = {
    name: PropTypes.string.isRequired,
  };
  
  export default MyComponent;
`;

const componentInfo = parse(componentCode);
console.log(componentInfo);
  1. Using a custom resolver:
import { parse, resolver } from 'react-docgen';

const customResolver = (ast) => {
  // Custom logic to find component definition
  // ...
  return resolver.findAllExportedComponentDefinitions(ast);
};

const componentInfo = parse(componentCode, customResolver);
  1. Extracting documentation from a file:
import { parseFile } from 'react-docgen';

parseFile('./src/components/MyComponent.js')
  .then((componentInfo) => {
    console.log(componentInfo);
  })
  .catch((error) => {
    console.error('Error parsing file:', error);
  });

Getting Started

To use react-docgen in your project:

  1. Install the package:

    npm install --save-dev react-docgen
    
  2. Create a script to extract component information:

    import { parse } from 'react-docgen';
    import fs from 'fs';
    
    const componentCode = fs.readFileSync('./src/components/MyComponent.js', 'utf8');
    const componentInfo = parse(componentCode);
    console.log(JSON.stringify(componentInfo, null, 2));
    
  3. Run the script to generate documentation:

    node extract-docs.js > component-docs.json
    

Competitor Comparisons

246,614

The library for web and native user interfaces.

Error generating comparison

Runtime type checking for React props and similar objects

Pros of prop-types

  • Lightweight and focused solely on runtime type checking
  • Easy to integrate into existing React projects
  • Provides clear error messages for type mismatches during development

Cons of prop-types

  • Limited to runtime type checking, doesn't provide static analysis
  • Requires manual definition of prop types for each component
  • Can impact performance if not stripped out in production builds

Code Comparison

prop-types:

import PropTypes from 'prop-types';

MyComponent.propTypes = {
  name: PropTypes.string.isRequired,
  age: PropTypes.number
};

react-docgen:

/**
 * @param {Object} props
 * @param {string} props.name
 * @param {number} [props.age]
 */
function MyComponent(props) {
  // Component logic
}

Key Differences

  • Purpose: prop-types focuses on runtime type checking, while react-docgen is used for generating documentation from component definitions
  • Usage: prop-types is used directly in component code, react-docgen is typically part of a build process
  • Output: prop-types provides runtime warnings, react-docgen generates static documentation

Use Cases

  • Use prop-types for immediate feedback during development and basic type safety
  • Use react-docgen for generating comprehensive documentation and API references for React components

Both tools serve different purposes in the React ecosystem and can be used complementarily to improve component development and documentation.

A simple parser for react properties defined in typescript instead of propTypes.

Pros of react-docgen-typescript

  • Better support for TypeScript, including type inference and handling of complex TypeScript types
  • More accurate parsing of PropTypes and default props in TypeScript files
  • Ability to generate documentation for styled-components

Cons of react-docgen-typescript

  • May have slower performance for large projects due to TypeScript parsing
  • Less extensive documentation compared to react-docgen
  • Potentially more complex setup for non-TypeScript projects

Code Comparison

react-docgen:

import React from 'react';
import PropTypes from 'prop-types';

const MyComponent = ({ name }) => <div>{name}</div>;

MyComponent.propTypes = {
  name: PropTypes.string.isRequired,
};

react-docgen-typescript:

import React from 'react';

interface Props {
  name: string;
}

const MyComponent: React.FC<Props> = ({ name }) => <div>{name}</div>;

export default MyComponent;

Both libraries aim to extract information from React components for documentation purposes. react-docgen is more suitable for JavaScript projects and has been around longer, while react-docgen-typescript is optimized for TypeScript projects and offers better type inference. The choice between the two depends on the project's primary language and specific documentation needs.

90,409

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation

Pros of Storybook

  • Provides a visual development environment for UI components
  • Supports multiple frontend frameworks (React, Vue, Angular, etc.)
  • Offers extensive addon ecosystem for enhanced functionality

Cons of Storybook

  • Steeper learning curve and more complex setup
  • Requires additional configuration for advanced features
  • Can increase project size and build time

Code Comparison

React-docgen:

import { parse } from 'react-docgen';

const componentInfo = parse(source);
console.log(componentInfo.props);

Storybook:

import { Button } from './Button';

export default {
  title: 'Components/Button',
  component: Button,
};

export const Primary = () => <Button primary>Click me</Button>;

React-docgen focuses on extracting component information programmatically, while Storybook provides a visual environment for component development and documentation. React-docgen is lightweight and easy to integrate into existing workflows, but lacks the interactive features of Storybook. Storybook offers a more comprehensive solution for component development, testing, and documentation, but requires more setup and resources. The choice between the two depends on project requirements, team preferences, and the desired level of visual component representation.

15,447

An API documentation generator for JavaScript.

Pros of JSDoc

  • Broader language support: Works with JavaScript, TypeScript, and other languages
  • Extensive documentation capabilities: Supports a wide range of tags for detailed documentation
  • Large community and ecosystem: Many plugins and tools available

Cons of JSDoc

  • Steeper learning curve: More complex syntax and numerous tags to learn
  • Potentially verbose: Can lead to lengthy documentation blocks in code

Code Comparison

JSDoc example:

/**
 * Calculates the sum of two numbers.
 * @param {number} a - The first number.
 * @param {number} b - The second number.
 * @returns {number} The sum of a and b.
 */
function add(a, b) {
  return a + b;
}

React-docgen example:

import PropTypes from 'prop-types';

/**
 * A button component.
 */
function Button({ label, onClick }) {
  return <button onClick={onClick}>{label}</button>;
}

Button.propTypes = {
  label: PropTypes.string.isRequired,
  onClick: PropTypes.func,
};

While JSDoc is a more general-purpose documentation tool, React-docgen is specifically designed for React components. JSDoc offers more flexibility and broader language support, but React-docgen provides a more streamlined experience for React projects with automatic prop type inference and component-specific documentation features.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

react-docgen

react-docgen is a highly customizable library that extracts information from React components and returns this information in a structured machine readable format from which documentations can be generated.

@react-docgen/cli is a cli wrapper around the library allowing using react-docgen on the command line.

Typescript

See this issue about the status of TypeScript.

Documentation

For version 5.x please checkout the README.md on the 5.x branch

For version 6.x and newer please visit react-docgen.dev

License

This project is licensed under the MIT License.

NPM DownloadsLast 30 Days