Skip to content

Commit

Permalink
[Docs] sort-comp: add class component examples
Browse files Browse the repository at this point in the history
  • Loading branch information
maurer2 authored and ljharb committed Jul 15, 2022
1 parent bdbd2a4 commit 87fb344
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [Refactor] [`jsx-indent-props`]: improved readability of the checkNodesIndent function ([#3315][] @caroline223)
* [Tests] [`jsx-indent`], [`jsx-one-expression-per-line`]: add passing test cases ([#3314][] @ROSSROSALES)
* [Refactor] `boolean-prop-naming`, `jsx-indent`: avoid assigning to arguments ([#3316][] @caroline223)
* [Docs] `sort-comp`: add class component examples ([#3339][] @maurer2)

[#3339]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3339
[#3331]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3331
[#3328]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3328
[#3327]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3327
Expand Down
60 changes: 60 additions & 0 deletions docs/rules/sort-comp.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ var Hello = createReactClass({
});
```

```jsx
class Hello extends React.Component {
render() {
return <div>Hello</div>;
}
static displayName = 'Hello';
}
```

Examples of **correct** code for this rule:

```jsx
Expand All @@ -35,6 +44,15 @@ var Hello = createReactClass({
});
```

```jsx
class Hello extends React.Component {
static displayName = 'Hello';
render() {
return <div>Hello</div>;
}
}
```

## Rule Options

This rule can take one argument to customize the components organisation.
Expand Down Expand Up @@ -128,6 +146,16 @@ var Hello = createReactClass({
});
```

```jsx
class Hello extends React.Component {
render() {
return <div>Hello</div>;
}
onClick = this.onClick.bind(this);
onClick() {}
}
```

Examples of **correct** code for this rule, with the above configuration:

```jsx
Expand All @@ -139,6 +167,16 @@ var Hello = createReactClass({
});
```

```jsx
class Hello extends React.Component {
onClick = this.onClick.bind(this);
onClick() {}
render() {
return <div>Hello</div>;
}
}
```

If you want to split your `render` method into smaller ones and keep them just before render:

```js
Expand Down Expand Up @@ -170,6 +208,17 @@ var Hello = createReactClass({
});
```

```jsx
class Hello extends React.Component {
renderButton = () => {}
onClick = this.onClick.bind(this);
onClick() {}
render() {
return <div>Hello</div>;
}
}
```

Examples of **correct** code for this rule, with the above configuration:

```jsx
Expand All @@ -182,6 +231,17 @@ var Hello = createReactClass({
});
```

```jsx
class Hello extends React.Component {
onClick = this.onClick.bind(this);
onClick() {}
renderButton = () => {}
render() {
return <div>Hello</div>;
}
}
```

If you want to flow annotations to be at the top:

```js
Expand Down

0 comments on commit 87fb344

Please sign in to comment.