React type checker creator itself should not be used as a prop type
- REACT_MISUSED_CREATOR_IN_PROP_TYPES
- Error
- Medium
- react
This rule applies when a React type checker creator itself is used as a prop type.
React type checker creator is a function type, so its return value should be used as a prop type.
Type checker creators supported in React are as follows:
PropTypes.instanceOf()PropTypes.oneOf()PropTypes.oneOfType()PropTypes.arrayOf()PropTypes.objectOf()PropTypes.shape()
When the above React type checker creator is misused, type checking for the prop is ignored and React outputs a warning message.
Note: This rule is based on React 17 API specifications.
Noncompliant Code Example
View with compliant examples side by sideimport React from 'react';
import PropTypes from 'prop-types';
export class Hello extends React.Component {
static propTypes = {
greetInfo: PropTypes.shape // REACT_MISUSED_CREATOR_IN_PROP_TYPES alarm because 'PropTypes.shape' is a type checker creator.
};
render() {
return <div>{this.props.greetInfo.greetName}</div>;
}
}Compliant Code Example
View with noncompliant examples side by sideimport React from 'react';
import PropTypes from 'prop-types';
export class Hello extends React.Component {
static propTypes = {
greetInfo: PropTypes.shape({
greetName: PropTypes.string
})
};
render() {
return <div>{this.props.greetInfo.greetName}</div>;
}
}Version
This rule was introduced in DeepScan 1.6.0-beta.
See
React Warning: type specification of prop is invalid; the type checker function must return
nullor anErrorbut returned a function. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).