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 "require .isRequired" rule #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
53 changes: 50 additions & 3 deletions react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,50 @@ export default class TextInput extends React.PureComponent {

**[⬆ back to top](#table-of-contents)**

### Required props
### Required Props

Any prop that does not have a default in `defaultProps` should be marked as `isRequired`. If you don't want to mark a prop as required, but don't want to provide a default, you instead provide a default value of `null`.


```js
// good
export default class TextInput extends React.PureComponent {
static propTypes = {
onChange: React.PropTypes.func.isRequired,
type: React.PropTypes.string,
required: React.PropTypes.bool,
defaultValue: React.PropTypes.string,
role: React.PropTypes.string,
}

static defaultProps = {
type: 'text',
required: false,
defaultValue: '',
role: null,
}
}

// bad (`required` is not marked as _required_ (the irony) but does not have a default &
// `role` is not marked as required, and even though we're okay with no value being provided,
// we haven't set the default to `null`)
export default class TextInput extends React.PureComponent {
static propTypes = {
onChange: React.PropTypes.func.isRequired,
type: React.PropTypes.string,
required: React.PropTypes.bool,
defaultValue: React.PropTypes.string,
role: React.PropTypes.string,
}

static defaultProps = {
type: 'text',
defaultValue: '',
}
}
```

### Props with Defaults

Don't declare any of the `propTypes` as required if they are included in `defaultProps`:

Expand All @@ -283,7 +326,9 @@ export default class TextInput extends React.PureComponent {

static defaultProps = {
type: 'text',
defaultValue: ''
required: false,
defaultValue: '',
role: null,
}
}

Expand All @@ -300,7 +345,9 @@ export default class TextInput extends React.PureComponent {

static defaultProps = {
type: 'text',
defaultValue: ''
required: false,
defaultValue: '',
role: null,
}
}
```
Expand Down