react-docgen icon indicating copy to clipboard operation
react-docgen copied to clipboard

Support Object.assign in propTypes definition

Open danielgefen opened this issue 8 years ago • 9 comments

In my React App, I have components which pass receive properties but just pass them as is to their child component. The parent component sort of extends the child component properties, so I'm using Object.assign to avoid code duplication. Unfortunately, recat-docgen doesn't seem to handle propTypes values which are evaluated from a function.

For example, let's say I have a component named ParentComponent and a component named ChildComponent.

The render function of the ParentComponent looks like this:

render() {
  return (
    <ChildComponent childPropA="SomeValue" childPropB="AnotherValue" />
  );
}

When I render the ParentComponent my code looks like this:

<ParentComponent parentPropA="Value" childPropA="SomeValue" childPropB="AnotherValue" />

Therefore, I use Object.assign for setting ParentComponent.propTypes:

ParentComponent.propTypes = Object.assign({}, ChildComponent.propTypes, {
  parentPropA: React.PropTypes.string
});

danielgefen avatar Jan 25 '17 17:01 danielgefen

I guess we could special case Object.assign, but a quick solution would be to use:

ParentComponent.propTypes = {
  ...ChildComponent.propTypes,
  parentPropA: React.PropTypes.string,
};

which react-docgen understands (and which is exactly the same).

fkling avatar Jan 25 '17 17:01 fkling

Thanks! I believe that this is my most common case, then this solution should do.

danielgefen avatar Jan 25 '17 17:01 danielgefen

With the new push for ES6 class syntax over the now-deprecated React.createClass(), this issue has become a little bit more prevalent. ES6 classes don't (yet) support static data members without a MyClass.myProp = ... outside of the main class definition. They do, however, support static getters, but that would mean returning something like propTypes from a function.

Full example:

class MyComponent extends React.Component {

  static get propTypes() {
    return {
      ...
    }
  }

}

vs.

class MyComponent extends React.Component {

}

MyComponent.propTypes = {
}

It would be really nice to be able to declare propTypes inside the ES6 class body and still have them be detected by react-docgen, although I still wouldn't call it absolutely essential.

bsmith-cycorp avatar Apr 13 '17 16:04 bsmith-cycorp

@bsmith-cycorp: Sorry for not have followed up on this! You are right, static get propTypes() doesn't work but it definitely should. Feel free to open a new issue about this.

fkling avatar May 08 '17 20:05 fkling

Unfortunately the the following syntax doesn't work:

import genericButtonProps from '../../common/buttons/genericButtonProps';

// ...

const buttonSpecificProps = {
  onClick: PropTypes.func,
  trigger: PropTypes.object,
  type: PropTypes.oneOf(['submit', 'reset', 'button', 'menu']),
};

Button.propTypes = { ...genericButtonProps, ...buttonSpecificProps };

Only properties defined in buttonSpecificProps are serialized. Of course I've tried the approach with Object.assign but it doesn't work too. It would be really nice if I could import props from another file.

michalstocki avatar Sep 05 '17 13:09 michalstocki

I also have hit this snag. It would be great if we could merge proptypes with spread operators / Object.assign

dan-kez avatar Mar 12 '18 22:03 dan-kez

Does anyone know if anything has come of this? This would be an incredible feature.

carrickjason avatar Jul 09 '18 17:07 carrickjason

Neither spread properties or object assign is working, is there a workaround ?

https://reactcommunity.org/react-docgen/

import React, { Component } from 'react';
import PropTypes from 'prop-types';

class MyParent extends Component {render() {}}
MyParent.propTypes = {
  /**
   * hello desc
   */
  hello: PropTypes.number,
}

class MyComponent extends Component {render() {}}
MyComponent.propTypes = {
  ...MyParent.propTypes,
  /**
   * foo desc
   */
  foo: PropTypes.number,
};

MyComponent.defaultProps = {
  foo: 42
};

export default MyComponent;

ylacaute avatar Dec 30 '19 10:12 ylacaute

I have the same problem, is this being worked on? @Tauana-Pacheco FYI

saviomd avatar Sep 15 '22 14:09 saviomd