|
1 | 1 | # Prevent usage of context (no-context)
|
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | ## Rule Details
|
6 | 6 |
|
7 | 7 | The following patterns are considered warnings:
|
8 | 8 |
|
9 |
| -```js |
| 9 | +```jsx |
10 | 10 | class Button extends React.Component {
|
11 | 11 | render() {
|
12 | 12 | return (
|
13 | 13 | <button style={{background: this.context.color}}>
|
14 |
| - {this.props.children} |
| 14 | + {this.props.text} |
15 | 15 | </button>
|
16 | 16 | );
|
17 | 17 | }
|
18 | 18 | }
|
| 19 | +Button.propTypes = { |
| 20 | + text: React.PropTypes.string |
| 21 | +}; |
19 | 22 | Button.contextTypes = {
|
20 | 23 | color: React.PropTypes.string
|
21 | 24 | };
|
| 25 | +``` |
22 | 26 |
|
23 |
| -const Button = ({children}, context) => |
| 27 | +```jsx |
| 28 | +const Button = ({text}, context) => |
24 | 29 | <button style={{background: context.color}}>
|
25 |
| - {children} |
| 30 | + {text} |
26 | 31 | </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 | +}; |
28 | 38 | ```
|
29 | 39 |
|
30 | 40 | The following patterns are not considered warnings:
|
31 | 41 |
|
32 |
| -```js |
| 42 | +```jsx |
33 | 43 | class Button extends React.Component {
|
34 | 44 | render() {
|
35 | 45 | return (
|
36 | 46 | <button style={{background: this.props.color}}>
|
37 |
| - {this.props.children} |
| 47 | + {this.props.text} |
38 | 48 | </button>
|
39 | 49 | );
|
40 | 50 | }
|
41 | 51 | }
|
42 | 52 | Button.propTypes = {
|
| 53 | + text: React.PropTypes.string, |
43 | 54 | color: React.PropTypes.string
|
44 | 55 | };
|
| 56 | +``` |
45 | 57 |
|
46 |
| -const Button = ({children, color}, context) => |
| 58 | +```jsx |
| 59 | +const Button = ({text, color}) => |
47 | 60 | <button style={{background: color}}>
|
48 |
| - {children} |
| 61 | + {text} |
49 | 62 | </button>;
|
50 |
| -Button.propTypes = {color: React.PropTypes.string}; |
| 63 | +Button.propTypes = { |
| 64 | + text: React.PropTypes.string, |
| 65 | + color: React.PropTypes.string |
| 66 | +}; |
51 | 67 | ```
|
0 commit comments