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

DefaultProps

Open Frikki opened this issue 9 years ago • 6 comments

How can you specify defaultProps on the ownee class?

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/13446506-defaultprops?utm_campaign=plugin&utm_content=tracker%2F16182690&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F16182690&utm_medium=issues&utm_source=github).

Frikki avatar May 07 '15 09:05 Frikki

Decorators hide static properties..

koistya avatar May 07 '15 10:05 koistya

Sorry, but I don’t understand your answer. I’d like to know how I can define propTypes and defaultProps on the component that is being decorated?

Frikki avatar May 07 '15 10:05 Frikki

In general you define default properties and their types like so:

class MyComponent {

  static propTypes: {
    message: PropTypes.string.isRequired
  };

  static defaultProps: {
    message: 'Welcome!'
  };

  render() {
    return <div>{this.props.message}</div>;
  }
}

...but the problem is, if you wrap this class into a higher-level component, HOC will hide static properties of the ownee class.

koistya avatar May 07 '15 11:05 koistya

Does that mean that it is useless to have propTypes and defaultProps defined on the ownee class? And how can we work around this without too much hacking? After all, the checks on propTypes and default provided values are quite important in many cases.

Frikki avatar May 07 '15 12:05 Frikki

@Frikki Got any solution for accessing the props?? Any work around

sandeepone avatar Aug 01 '15 17:08 sandeepone

class MyComponent {
  props: {
    message: 'Welcome!'
  };
}

iegik avatar Nov 13 '17 12:11 iegik