Skip to content
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

Add examples of correctly specified propTypes #813

Merged
merged 1 commit into from
Sep 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/rules/prop-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ function Hello({ name }) {
}
```

Examples of correct usage without warnings:

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

// Or in ES6:
class HelloEs6 extends React.Component {
render() {
return <div>Hello {this.props.name}</div>;
}
}
HelloEs6.propTypes = {
name: React.PropTypes.string.isRequired,
};

// ES6 + Public Class Fields (draft: https://tc39.github.io/proposal-class-public-fields/)
class HelloEs6WithPublicClassField extends React.Component {
static propTypes = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't actually ES6 - it's using a stage 2 proposal for public class properties.

The current standard JS way of doing this is Hello.propTypes = { … }, after the class definition.

name: React.PropTypes.string.isRequired,
}
render() {
return <div>Hello {this.props.name}</div>;
}
}
```

The following patterns are not considered warnings:

```jsx
Expand Down