Skip to content

Commit

Permalink
docs: update documentation examples to work with react-docgen@5
Browse files Browse the repository at this point in the history
  • Loading branch information
ZauberNerd committed Dec 15, 2019
1 parent c986d19 commit d39fad0
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -18,13 +21,15 @@ 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
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).

Expand All @@ -43,27 +48,31 @@ 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';
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 <div />;
}
Expand All @@ -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 <div />;
}
Expand Down

0 comments on commit d39fad0

Please sign in to comment.