Skip to content

Commit

Permalink
Merge 4fd80db into 80f3826
Browse files Browse the repository at this point in the history
  • Loading branch information
mrflip committed Apr 4, 2020
2 parents 80f3826 + 4fd80db commit 372d286
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 6 deletions.
5 changes: 1 addition & 4 deletions docs/rules/no-this-in-sfc.md
@@ -1,9 +1,6 @@
# Prevent `this` from being used in stateless functional components (react/no-this-in-sfc)

When using a stateless functional component (SFC), props/context aren't accessed in the same way as a class component or the `create-react-class` format. Both props and context are passed as separate arguments to the component instead. Also, as the name suggests, a stateless component does not have state on `this.state`.

Attempting to access properties on `this` can be a potential error if someone is unaware of the differences when writing a SFC or missed when converting a class component to a SFC.

In React, there are two styles of component. One is a class component: `class Foo extends Component {...}`, which accesses its props, context and state as properties of `this`: `this.props.foo`, etc. The other are stateless functional components (SFCs): `function Foo(props, context) {...}`. As you can see, there's no `state` (hence the name), and the props and context are provided as its two functional arguments. In an SFC, state is usually best implements with a [React hook](https://reactjs.org/docs/hooks-overview.html) such as `React.useState()`. Attempting to access properties on `this` can sometimes be valid, but it's very commonly an error caused by unfamiliarity with the differences between the two styles of components, or a missed reference when converting a class component to an SFC.

## Rule Details

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-this-in-sfc.js
Expand Up @@ -11,7 +11,7 @@ const docsUrl = require('../util/docsUrl');
// Constants
// ------------------------------------------------------------------------------

const ERROR_MESSAGE = 'Stateless functional components should not use this';
const ERROR_MESSAGE = 'Stateless functional components should not use `this`';

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/no-this-in-sfc.js
Expand Up @@ -8,7 +8,7 @@
// Constants
// ------------------------------------------------------------------------------

const ERROR_MESSAGE = 'Stateless functional components should not use this';
const ERROR_MESSAGE = 'Stateless functional components should not use `this`';

// ------------------------------------------------------------------------------
// Requirements
Expand Down

0 comments on commit 372d286

Please sign in to comment.