React finds componentDidMount, propTypes, and defaultProps by name. If you write componentDidmount or PropTypes, nothing fails. React never calls the method and never checks the props.
The rule reports four kinds of mistakes in a component:
- A lifecycle method, such as
componentDidMountorshouldComponentUpdate, with the wrong capitalization. getDerivedStateFromPropswithoutstatic.propTypes,contextTypes,childContextTypes, ordefaultPropswith the wrong capitalization.- A type that the
prop-typespackage doesn't have, such asPropTypes.boolean, or anything other than.isRequiredafter a type. This only works if the file importsprop-typesorreact.
It also reports import "react" and import "prop-types", which don't import any names.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"react/no-typos" = "error" # or "warn"
Examples
Reported:
class Clock extends React.Component {
static DefaultProps = { interval: 1000 };
getDerivedStateFromProps(props) {
return { interval: props.interval };
}
componentDidmount() {
this.start();
}
}
import PropTypes from "prop-types";
class Clock extends React.Component {
static propTypes = {
running: PropTypes.boolean,
interval: PropTypes.number.isrequired,
};
}
Not reported:
import PropTypes from "prop-types";
class Clock extends React.Component {
static propTypes = {
running: PropTypes.bool,
interval: PropTypes.number.isRequired,
};
static defaultProps = { interval: 1000 };
static getDerivedStateFromProps(props) {
return { interval: props.interval };
}
componentDidMount() {
this.start();
}
}