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

React.PropTypes.node should accept stateless component (function) #5527

Closed
MoOx opened this issue Nov 22, 2015 · 3 comments
Closed

React.PropTypes.node should accept stateless component (function) #5527

MoOx opened this issue Nov 22, 2015 · 3 comments

Comments

@MoOx
Copy link

MoOx commented Nov 22, 2015

I just replaced some react components that where defined using es6 classes and I am now getting warnings about my react stateless component (that are just fuction) not being a ReactNode.

Warning: Failed propType: Invalid prop `Loader` supplied to `Picture`, expected a ReactNode.

I were validating some components using React.PropTypes.node, but now I need to use PropTypes.oneOfType([ PropTypes.node, PropTypes.func ]) which seems a bit long to say "this can be anything that can be rendered as a component" (which is what ProTypes.node is supposed to cover right?).

From the doc

    // Anything that can be rendered: numbers, strings, elements or an array
    // (or fragment) containing these types.
    optionalNode: React.PropTypes.node,

Maybe func should be added? Or is there any reason why node doesn't handle func as well?

@zdenekkostal
Copy link

What about adding new propType validator? Something like

propTypes: {
    optionalComponent: React.PropTypes.component
}

where component would have form of

PropTypes.oneOfType([ PropTypes.node, PropTypes.func ])

or something similar. In my opinion it is quite dangerous to let people pass in functions, but I'm not sure how to make better validation. Reading function.toString seems like a gigantic hack which would not work after minification, but this is the only option I'm aware of.

@zpao
Copy link
Member

zpao commented Nov 23, 2015

Can you show how you were using this? I don't think Component Classes should have been valid for node either. node is supposed to accept anything that can be rendered. Classes can't be rendered directly - you would pass an element with the type set.

@MoOx
Copy link
Author

MoOx commented Nov 23, 2015

Oh my god. You are so right. I feel stupid. I guess I was validating something the bad way. Sorry for the noise.

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

3 participants