-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
400218a
commit 996008f
Showing
1 changed file
with
27 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -411,32 +411,42 @@ See: [Compound State](#compound-state) pattern | |
## Existence Checking | ||
Do not check existence of `prop` objects. Use `defaultProps`. | ||
Do not check existence of props at the root of a component. | ||
Componenst should not have two possible return types. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
```javascript | ||
// bad | ||
render () { | ||
if (this.props.person) { | ||
return <div>{this.props.person.firstName}</div>; | ||
} else { | ||
return null; | ||
} | ||
const Person = props => { | ||
if (this.props.firstName) | ||
return <div>{this.props.firstName}</div> | ||
else | ||
return null | ||
} | ||
``` | ||
Componenents should *always* render. Consider adding `defaultProps`, where a sensible default is appropriate. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
chantastic
Author
Owner
|
||
```javascript | ||
// good | ||
class MyComponent extends React.Component { | ||
render() { | ||
return <div>{this.props.person.firstName}</div>; | ||
} | ||
// better | ||
const Person = props => | ||
<div>{this.props.firstName}</div> | ||
|
||
Person.defaultProps = { | ||
firstName: "Guest" | ||
} | ||
``` | ||
MyComponent.defaultProps = { | ||
person: { | ||
firstName: 'Guest' | ||
} | ||
}; | ||
If a component should be conditionally rendered, handle that in the owner component. | ||
```javascript | ||
// best | ||
const TheOwnerComponent = props => | ||
<div> | ||
{props.person | ||
? <Person {...props.person} /> | ||
: null | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
</div> | ||
``` | ||
This is only where objects or arrays are used. Use PropTypes.shape to clarify | ||
|
typo:
Componenst
->Components