-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Favor stateless functions over classes when there's no state #688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
## Table of Contents | ||
|
||
1. [Basic Rules](#basic-rules) | ||
1. [Class vs `React.createClass`](#class-vs-reactcreateclass) | ||
1. [Class vs `React.createClass` vs stateless](#class-vs-reactcreateclass-vs-stateless) | ||
1. [Naming](#naming) | ||
1. [Declaration](#declaration) | ||
1. [Alignment](#alignment) | ||
|
@@ -27,25 +27,44 @@ | |
|
||
## Class vs `React.createClass` | ||
|
||
- Use `class extends React.Component` unless you have a very good reason to use mixins. | ||
- If you have internal state and/or refs, prefer `class extends React.Component` over `React.createClass` unless you have a very good reason to use mixins. | ||
|
||
eslint rules: [`react/prefer-es6-class`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-es6-class.md). | ||
|
||
```javascript | ||
// bad | ||
const Listing = React.createClass({ | ||
render() { | ||
return <div />; | ||
// ... | ||
render: function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. per our other linting rules, this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
return <div>{this.state.hello}</div>; | ||
} | ||
}); | ||
|
||
// good | ||
class Listing extends React.Component { | ||
// ... | ||
render() { | ||
return <div />; | ||
return <div>{this.state.hello}</div>; | ||
} | ||
} | ||
``` | ||
|
||
And if you don't have state or refs, prefer functions over classes: | ||
|
||
```javascript | ||
|
||
// bad | ||
class Listing extends React.Component { | ||
render() { | ||
return <div>{this.props.hello}</div>; | ||
} | ||
} | ||
|
||
// good | ||
function Listing ({ hello }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be no space after listing (so this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, good catch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an example of why #685 would be awesome. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
return <div>{hello}</div>; | ||
} | ||
``` | ||
|
||
## Naming | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's unfortunate that this will break the link - however since the title on line 28 didn't change, are you sure this link works now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now it works :D