Skip to content

Commit

Permalink
Clean up examples in prop-types docs
Browse files Browse the repository at this point in the history
These examples use `createReactClass` a lot, which is legacy at this
point. I'm modifying examples to primarily use function components
because those are the ones people are using most often today. Other ways
of defining a component are just here to show that this rule recognizes
them, too.
  • Loading branch information
silvenon committed Jan 18, 2020
1 parent a7f6a8b commit 932d947
Showing 1 changed file with 27 additions and 33 deletions.
60 changes: 27 additions & 33 deletions docs/rules/prop-types.md
Expand Up @@ -9,29 +9,43 @@ It can warn other developers if they make a mistake while reusing the component
The following patterns are considered warnings:

```jsx
var Hello = createReactClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
function Hello({ name }) {
return <div>Hello {name}</div>;
// 'name' is missing in props validation
}

var Hello = createReactClass({
propTypes: {
firstname: PropTypes.string.isRequired
},
render: function() {
return <div>Hello {this.props.firstname} {this.props.lastname}</div>; // lastname type is not defined in propTypes
return <div>Hello {this.props.firstname} {this.props.lastname}</div>;
// 'lastname' type is missing in props validation
}
});

function Hello({ name }) {
return <div>Hello {name}</div>;
// Or in ES6
class Hello extends React.Component {
render() {
return <div>Hello {this.props.firstname} {this.props.lastname}</div>;
// 'lastname' type is missing in props validation
}
}
Hello.propTypes = {
firstname: PropTypes.string.isRequired
}
```

Examples of correct usage without warnings:

```jsx
function Hello({ name }) {
return <div>Hello {name}</div>;
}
Hello.propTypes = {
name: PropTypes.string.isRequired
}

var Hello = createReactClass({
propTypes: {
name: PropTypes.string.isRequired,
Expand Down Expand Up @@ -65,35 +79,15 @@ class HelloEs6WithPublicClassField extends React.Component {
The following patterns are **not** considered warnings:

```jsx
var Hello = createReactClass({
render: function() {
return <div>Hello World</div>;
}
});

var Hello = createReactClass({
propTypes: {
name: PropTypes.string.isRequired
},
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
function Hello() {
return <div>Hello World</div>;
}

// Referencing an external object disable the rule for the component
var Hello = createReactClass({
propTypes: myPropTypes,
render: function() {
return <div>Hello {this.props.name}</div>;
}
});

function Hello({ name }) {
return <div>Hello {name}</div>;
}
Hello.propTypes = {
name: PropTypes.string.isRequired,
};
Hello.propTypes = myPropTypes;
```

## Rule Options
Expand Down Expand Up @@ -135,6 +129,6 @@ For this rule to work we need to detect React components, this could be very har

For now we should detect components created with:

* a function that return JSX or the result of a `React.createElement` call.
* `createReactClass()`
* an ES6 class that inherit from `React.Component` or `Component`
* a stateless function that return JSX or the result of a `React.createElement` call.

0 comments on commit 932d947

Please sign in to comment.