react/no-this-in-sfc
Disallow this in function components
A function component gets its props as an argument. React calls it without a this, so this.props throws a TypeError. This often happens when a class component is rewritten as a function and one this. is left behind.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"react/no-this-in-sfc" = "error" # or "warn"Examples#
example.jsx1 problem
function Greeting(props) {
return <h1>Hello, {this.propsStateless functional components should not use this.name}</h1>;
}example.jsxNo problems
function Greeting(props) {
return <h1>Hello, {props.name}</h1>;
}
class Welcome extends React.Component {
render() {
return <h1>Welcome, {this.props.name}</h1>;
}
}