-
Notifications
You must be signed in to change notification settings - Fork 359
Ability to reflect on prop-type instances? #161
Comments
i need too,use in storybook knobs,reflect prop-types can auto generate knobs easily. |
Given a proptype value specified on an object, you can find the key it comes from via a brute force search. This only works for the basic prop types (ie. not things like import PropTypes from 'prop-types';
import findKey from 'lodash/findKey';
MyButton.propTypes = {
disabled: PropTypes.bool
onClick: PropTypes.func.isRequired
};
function getPropTypeName(typeEntry) {
return findKey(PropTypes, p => p === typeEntry || p.isRequired === typeEntry);
}
getPropTypeName(MyButton.propTypes.disabled); // 'bool'
getPropTypeName(MyButton.propTypes.onClick); // 'func' |
https://npmjs.com/find-value-locations can help with that |
@cpimhoff @ljharb Thanks for the help on a partial solution. Looking up prop types by reference is certainly better than nothing 😸 However, full reflection is unfortunately required for my usecases. This includes reflecting on nested prop types and other prop types with custom constructor arguments. |
Duplicate of #60. |
Hello!
I'm looking for a way to reflect on generated prop-type instances. The purpose is for use in developer tooling.
The problem is that the generated instances are opaque functions with any meaningful metadata hidden in closures. I could maybe see this by design to discourage monkey-patching, but it's closing the door on some cool possibilities.
Example: https://runkit.com/awfulaxolotl/lack-of-reflection-in-prop-types
A use case: sullvn/tastes#6
So I guess my question comes down to:
Thanks!
The text was updated successfully, but these errors were encountered: