From d39fad062371ad8515c848f399d5503bfb83c6b7 Mon Sep 17 00:00:00 2001 From: ZauberNerd Date: Sun, 15 Dec 2019 14:14:02 +0100 Subject: [PATCH] docs: update documentation examples to work with react-docgen@5 --- README.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 65bd190..55961c8 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ -# react-docgen-displayname-handler [![Build Status](https://travis-ci.org/nerdlabs/react-docgen-displayname-handler.svg?branch=master)](https://travis-ci.org/nerdlabs/react-docgen-displayname-handler) +# react-docgen-displayname-handler ![Build Status](https://img.shields.io/travis/nerdlabs/react-docgen-displayname-handler/master) ![Latest published version](https://img.shields.io/npm/v/react-docgen-displayname-handler) + A handler for react-docgen that tries to infer the displayName of a component. ## Rationale + [react-docgen](https://github.com/reactjs/react-docgen) is a CLI and API toolbox to help extract information from [React](http://facebook.github.io/react/) components and generate documentation from it. @@ -10,6 +12,7 @@ components and generate documentation from it. used to infer the displayName for a given component. The handler tries to infer the displayName from the following sources: + - a static `displayName` property on the object / class - the name of the function or class declaration/expression - the name of the variable the component is assigned to @@ -18,6 +21,7 @@ The handler tries to infer the displayName from the following sources: - if everything fails the displayName is set to `UnknownComponent` ## Installation + Install `react-docgen-displayname-handler` from [npm](https://www.npmjs.com/package/react-docgen-displayname-handler) ```shell @@ -25,6 +29,7 @@ npm install --save react-docgen-displayname-handler ``` ## Usage + Unfortunately there is currently no easy way to use custom handlers with the [react-docgen CLI](https://github.com/reactjs/react-docgen#cli). @@ -43,8 +48,10 @@ const resolver = reactDocs.resolver.findExportedComponentDefinition; const handlers = reactDocs.handlers.concat(displayNameHandler); const documentation = reactDocs.parse(src, resolver, handlers); ``` + If you want to use the filepath for displayName resolution too, check out the following example: + ```javascript import reactDocs from 'react-docgen'; import { createDisplayNameHandler } from 'react-docgen-displayname-handler'; @@ -52,18 +59,20 @@ const resolver = reactDocs.resolver.findExportedComponentDefinition; const handlers = reactDocs.handlers.concat(createDisplayNameHandler(filePath)); const documentation = reactDocs.parse(src, resolver, handlers); ``` + In order to use the file path too, you need to import the named export `createDisplayNameHandler` which takes as an argument the file path and returns a handler function that can be passed to react-docgen. ## Examples + When using this custom handler with `react-docgen` it will try to find the displayName of the component as outlined above. ```javascript import React from 'react'; -export default class X { - static displayName = 'MyComponent' +export default class X extends React.Component { + static displayName = 'MyComponent'; render() { return
; } @@ -79,7 +88,7 @@ export default class X { ```javascript import React from 'react'; -export default class MyComponent { +export default class MyComponent extends React.Component { render() { return
; }