Skip to content

Commit 83b94d5

Browse files
committed
no-context: improve documentation
1 parent 4611c86 commit 83b94d5

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

docs/rules/no-context.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,67 @@
11
# Prevent usage of context (no-context)
22

3-
[Context](https://facebook.github.io/react/docs/context.html) is an advanced and experimental feature. The API is likely to change in future releases. Most applications will never need to use context. Especially if you are just getting started with React, you likely do not want to use context. Using context will make your code harder to understand because it makes the data flow less clear. It is similar to using global variables to pass state through your application.
3+
[Context](https://facebook.github.io/react/docs/context.html) is an advanced and experimental feature. The API is likely to change in future releases. Most applications will never need to use context. Especially if you are just getting started with React, you likely do not want to use context. Using context will make your code harder to understand because it makes the data flow less clear. It is similar to using global variables to pass state through your application. Consider higher order components or Flux architecture instead.
44

55
## Rule Details
66

77
The following patterns are considered warnings:
88

9-
```js
9+
```jsx
1010
class Button extends React.Component {
1111
render() {
1212
return (
1313
<button style={{background: this.context.color}}>
14-
{this.props.children}
14+
{this.props.text}
1515
</button>
1616
);
1717
}
1818
}
19+
Button.propTypes = {
20+
text: React.PropTypes.string
21+
};
1922
Button.contextTypes = {
2023
color: React.PropTypes.string
2124
};
25+
```
2226

23-
const Button = ({children}, context) =>
27+
```jsx
28+
const Button = ({text}, context) =>
2429
<button style={{background: context.color}}>
25-
{children}
30+
{text}
2631
</button>;
27-
Button.contextTypes = {color: React.PropTypes.string};
32+
Button.propTypes = {
33+
text: React.PropTypes.string
34+
};
35+
Button.contextTypes = {
36+
color: React.PropTypes.string
37+
};
2838
```
2939

3040
The following patterns are not considered warnings:
3141

32-
```js
42+
```jsx
3343
class Button extends React.Component {
3444
render() {
3545
return (
3646
<button style={{background: this.props.color}}>
37-
{this.props.children}
47+
{this.props.text}
3848
</button>
3949
);
4050
}
4151
}
4252
Button.propTypes = {
53+
text: React.PropTypes.string,
4354
color: React.PropTypes.string
4455
};
56+
```
4557

46-
const Button = ({children, color}, context) =>
58+
```jsx
59+
const Button = ({text, color}) =>
4760
<button style={{background: color}}>
48-
{children}
61+
{text}
4962
</button>;
50-
Button.propTypes = {color: React.PropTypes.string};
63+
Button.propTypes = {
64+
text: React.PropTypes.string,
65+
color: React.PropTypes.string
66+
};
5167
```

0 commit comments

Comments
 (0)