New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prop validation for Component Class #3077
Comments
You can use ReactPropTypes.any, which basically disables typechecking and lets you pass in anything you want. I'm not sure if it's public API (so take this with a grain of salt / it might be subject to change at any time without notice / be careful), but... there is nothing magical about the default proptype validators. They are just functions that validate a property (throwing an error if the property is invalid), and so you can write your own validator. To get started, you can look at the implementation of our default validators: https://github.com/facebook/react/blob/master/src/classic/types/ReactPropTypes.js Closing this issue out, since it's not technically a bug. For future reference, Stackoverflow is the best place for questions that are not bugs. |
I can write a custom validator that does what I want. I suppose my real question is weather or not React is discouraging me from using Component Constructors as props for some architectural reason? |
Yeah, I'm very surprised by this too. There have been numerous instances when I want to validate that a prop is something I can pass as the first argument to |
@skiano No, I don't think there is any fundamental architectural reason, but it shouldn't be a super common pattern. The more common pattern is to create the element on the outside and pass in the element, or to pass in a function that takes in a particular set of parameters and returns an element. This allows the owner/parent to have a little more control over the props that are passed to the nested elements, since the parent/owner is deciding the class of the element that is being created. |
You can get more specific that |
Oh, I forget, now that there are pure function components there really is no way to distinguish functions from components, is there? |
No |
Hello,
This is just a question.
I have a component that accepts a component constructor as a prop, and I noticed that while there is support for
React.PropTypes.element
, there is no PropType documented for checking if the prop is a React Class.Am I missing something? or is there a reason why I should not use props this way?
The text was updated successfully, but these errors were encountered: