Feature request - PropType.*.name
See original GitHub issueFeature 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:
- Created 7 years ago
- Reactions:2
- Comments:12 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
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:
This case is OK, but:
This case is a prop type collision.
But now this prop types matching is not reachable.