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
[v6] PropTypes not checked in custom elements ? #871
Comments
I'm not sure what you mean by that. You can set Input.propTypes = {
label: PropTypes.string.isRequired
} and then not pass Is that what you mean by "does not work"? |
@erikras yes, even a simple element test: import React, { PropTypes } from 'react'
const Input = (props) => (
<div className="form-group">
<label htmlFor={props.name}>{props.label}</label>
<input id={props.name} {...props} />
</div>
)
Input.propTypes = {
name: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
type: PropTypes.string.isRequired
}
export default Input This gives the warning: <Input /> This does not: <Field name="title" component={Input} /> |
Very interesting. Will investigate. 🔍 |
Crazy. Stateless Functionsconst EmailInput = props => <input type="email" {...props}/>
EmailInput.propTypes = { placeholder: React.PropTypes.string.isRequired } This gives a prop types warning: <Field name="email" component={props =>
<EmailInput {...props}/>
}/> And this does not: <Field name="email" component={EmailInput}/> Component ClassesIf you do this class EmailInput extends React.Component {
render() {
return <input type="email" {...this.props}/>
}
}
EmailInput.propTypes = { placeholder: React.PropTypes.string.isRequired } ...it works fine even with the <Field name="email" component={EmailInput}/> ...syntax. ConclusionThere must be something that the JSX syntax is doing that is running the stateless function through a prop types check. If you give We need a React expert. @gaearon, got a sec? |
I don't quite understand. Can you show me an example that doesn't validate PropTypes without Redux Form API? As in, just a standalone example demonstrating the problem. |
The problem is here: isClass(component) ? React.createFactory(component) : component You are bypassing React completely by calling functional components directly. This way their props are not checked, they don't appear in DevTools, and React has no knowledge about them. Generally this shouldn't be necessary. Just always use |
Here's my fiddle that I was making when you looked at the code yourself. :-) |
Changing my fiddle code to const Field = props => {
const { component, ...rest } = props
const Component = component
return <Component {...rest}/>
} is no good, because then it fails with the const Field = props => {
const { component, ...rest } = props
const factory = React.createFactory(component)
return factory(rest)
} is also no good for the same reason. Is there an |
My point is you don’t ever need |
Yes, I understand that, but I also want to be able to accept factories, like Perhaps I have to provide my own stateless component wrappers for the input factories, e.g. import { input, textarea, select } from 'redux-form'
...
<Field component={input}/> ...but I have some users who are not using JSX and therefore (I think) would find factory support useful. |
This is what const Field = props => {
const { component, ...rest } = props
return React.createElement(component, rest);
} Factories are not component types. They are just helpers for getting an element.
|
In your case, |
Excellent!! Cheers. |
Glad you got to look at this @ooflorent. You're the one not using JSX, right? Nothing about not accepting factories bothers you? |
Well, not really. I'll just wrap |
Since React encourages wrapping with factories at the import site I don't see a problem for factory users either. For native components, they would pass a string. For custom components, they would pass an unwrapped type that they import anyway. |
Fix published as |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hey, I've got a custom component and want to check the passed props:
In
Input
component I want to make sure that label prop is passed, but the propTypes does not work in Input element. Am I doing something wrong?The text was updated successfully, but these errors were encountered: