no-this-in-sfc
Configuration
Rule Details
Disallow this from being used inside stateless functional components (SFCs).
React supports two component styles: class components access instance state and
props through this (e.g. this.props.foo), and functional components receive
their props as the first argument. Reaching for this.props / this.state
inside a functional component is almost always a mistake — typically an
unfinished migration from a class component — because this is not bound to
the component instance.
The rule covers this.x and this['x'] access (PropertyAccess and
ElementAccess forms), bracket access included, parens transparent. It targets
the same set of stateless components as eslint-plugin-react: capitalized
function declarations / expressions / arrows that return JSX or null,
React.memo / React.forwardRef (and their bare aliases) wrapped components,
and anonymous export default function() { ... } components.
A this access inside a class component (an ES6 class extending
Component / PureComponent) or inside a createReactClass ES5 component
is never reported — those bind this to the component instance.
A this access inside a function that happens to be a property value of an
object literal (e.g. { Foo() { return <div>{this.x}</div> } }) is also not
reported, mirroring upstream's "Property" carve-out.
Examples of incorrect code for this rule:
Examples of correct code for this rule: