question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request - PropType.*.name

See original GitHub issue

Feature request: Fill name attribute or add static toString() method for all PropTypes.

For example:

const StringPropType = PropTypes.string;
expect(StringPropType.name).toEqual('checkType: String');

const ArrayOfBoolType = PropTypes.arrayOf(PropTypes.bool);
expect(ArrayOfBoolType.name).toEqual('bound checkType: Array of Bool');

const ShapeType = PropTypes.shape({
  foo: PropTypes.bool,
  bar: PropTypes.number
});
expect(ShapeType.name).toEqual('bound checkType: Shape of foo:Bool, bar:Number'

It maybe useful when debugging. In my case - I need to equal components prop types and throw exception if there are same props. So if generate names for prop types - they could be compared.

Current behaviour

Only primitive types could be compared (string, bool, number). Complex types (shape, arrayOf) - each time returns new bound function. So it could not be compared by value.

Problems with custom prop types

Will generate “undefined” name for custom prop like this:

customProp: function(props, propName, componentName) {
    if (!/matchme/.test(props[propName])) {
      return new Error(
        'Invalid prop `' + propName + '` supplied to' +
        ' `' + componentName + '`. Validation failed.'
      );
    }
  },

but it fixed by adding function name:

customProp: function customProp(props, propName, componentName) {

btw. Same feature implemented at tcomb lib

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
oleblaesingcommented, Jan 17, 2017

I want to create a small faker module for React that generates random data based on the components propTypes. This feature would be very helpful, because there is currently no “native” solution to handle this problem in a comfortable way. You can look at my current idea of implementation here: oleblaesing/react-proptypes-faker

2reactions
tuchk4commented, Dec 13, 2016

Hi @gaearon. Sorry for late response.

I will provide few examples when this is feature is useful

React storybook and react storybook info addon

Info addon parses all components from story and show table with component prop types and default props. And it work correctly with all simple types (number, string, node) and with isRequired flag.

Link to PropTypesMap generation https://github.com/storybooks/react-storybook-addon-info/blob/89bc19038c09b0b3439f5abb8de91315228d47e5/src/components/PropTable.js#L4

So if implement requested feature - this addon will work more correctly and for all prop types.

Forgekit

Forgekit merge prop types and default props from component and components features. But sometimes there are collision between defined prop type names - when same prop types defined in few features. But if prop type is also the same - probably it is not a collision.

I mean:

  • feature1:
foo: PropTypes.string
  • feature2:
foo: PropTypes.string

This case is OK, but:

  • feature1:
foo: propTypes.string
  • feature2:
foo: propTypes.bool

This case is a prop type collision.

But now this prop types matching is not reachable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typechecking With PropTypes - React
To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends ......
Read more >
Mastering Props And PropTypes In React - Smashing Magazine
This tutorial will introduce you to the details about props, passing and accessing props, and passing information to any component using props.
Read more >
React Basis: Default Props and Typechecking with PropTypes.
In this blog we review two React fundamental topics including defaults props and typechecking with PropTypes. Props are objects passed from ...
Read more >
Get the type of React components propTypes definition
Solution: I propose you test the names of the functions. This will prove that this is a valid React Prop type. // returns...
Read more >
Packaging and PropTypes - Fullstack React
From within this prop , we can define an object which has the key of a prop as the name of the prop...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found