Skip to content
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

Closed
skiano opened this issue Feb 6, 2015 · 7 comments
Closed

Prop validation for Component Class #3077

skiano opened this issue Feb 6, 2015 · 7 comments

Comments

@skiano
Copy link

skiano commented Feb 6, 2015

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?

@jimfb
Copy link
Contributor

jimfb commented Feb 6, 2015

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.

@jimfb jimfb closed this as completed Feb 6, 2015
@skiano
Copy link
Author

skiano commented Feb 6, 2015

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?

@jedwards1211
Copy link
Contributor

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 React.createElement.

@jimfb
Copy link
Contributor

jimfb commented Oct 19, 2015

@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.

@zpao
Copy link
Member

zpao commented Oct 19, 2015

You can get more specific that any. The real reason is that this "type" is a bit hard to define and actually cover all the cases. See my comments in #5143 (comment)

@jedwards1211
Copy link
Contributor

Oh, I forget, now that there are pure function components there really is no way to distinguish functions from components, is there?

@zpao
Copy link
Member

zpao commented Oct 20, 2015

No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants