-
Notifications
You must be signed in to change notification settings - Fork 48.4k
Add a section about es6 object shorthand #9115
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,6 +245,32 @@ function App2() { | |
|
||
Spread attributes can be useful when you are building generic containers. However, they can also make your code messy by making it easy to pass a lot of irrelevant props to components that don't care about them. We recommend that you use this syntax sparingly. | ||
|
||
### ES6 Object Shorthand | ||
|
||
If you want to filter the props that you want to pass, you can do it by using ES6 Object Shorthand in combination with object spread operator and object destructuring assignment: | ||
|
||
```js{4} | ||
render(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: need space before |
||
const {myPropsOne, myPropsTwo} = this.props; | ||
return ( | ||
<MyComponent {...{myPropsOne, myPropsTwo}} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use more realistic prop names than |
||
); | ||
} | ||
``` | ||
This is roughly equivalent to a code that looks like this: | ||
```js{4} | ||
render(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: need space before |
||
const spreadProps = { | ||
myPropsOne: this.props.myPropsOne, | ||
myPropsTow: this.props.myPropsTwo | ||
}; | ||
return ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would just type props manually in the second example. <MyComponent
myPropsOne={this.myPropsOne}
myPropsTwo={this.myPropsTwo} /> |
||
<MyComponent {...spreadProps} /> | ||
); | ||
} | ||
``` | ||
So you can extract only the required props for `MyComponent`. | ||
|
||
## Children in JSX | ||
|
||
In JSX expressions that contain both an opening tag and a closing tag, the content between those tags is passed as a special prop: `props.children`. There are several different ways to pass children: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please link to MDN.