1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-06 16:48:01 +00:00

Add Destructure Variables As Props To A Component as a react til

This commit is contained in:
jbranchaud
2018-02-20 13:03:22 -06:00
parent 4916e960ef
commit f5da3abc60
2 changed files with 47 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
# Destructure Variables As Props To A Component
When passing down props, a redundant-feeling pattern can sometimes emerge.
```javascript
const MyComponent = ({ handleChange, handleBlur }) => {
return (
<div>
<OtherComponent />
<MySubComponent handleChange={handleChange} handleBlur={handleBlur} />
</div>
)
};
```
The typing feel duplicative, as if there ought to be a better way. One
option is to simply pass down all the props:
```javascript
<MySubComponent {...props} />
```
This approach may result in passing down props that we don't intend to pass
down and clutters the flow of data in our app.
Here is another approach:
```javascript
const MyComponent = ({ handleChange, handleBlur }) => {
return (
<div>
<OtherComponent />
<MySubComponent {...{handleChange, handleBlur}} />
</div>
)
};
```
Here we are taking advantage of two ES6 features. Since the naming is the
same, we can use [property
shorthands](http://es6-features.org/#PropertyShorthand). Then we immediately
use the [spread operator](http://es6-features.org/#SpreadOperator) to splat
it back out as the props to the component.
h/t Vidal Ekechukwu