Skip to content

Commit dff1f06

Browse files
committed
destructuring
1 parent 5b443f5 commit dff1f06

4 files changed

+149
-1
lines changed

React/destructuring_basics-js.md

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
### Basics application of Destructuring
2+
3+
4+
Without using the destructuring syntax getting multiple values out of an array can be quite cumbersome. You would do something like this:
5+
6+
7+
```js
8+
const items = ['car', 'bike', 'plane'];
9+
10+
const car = items[0];
11+
const bike = items[1];
12+
const plane = items[2];
13+
14+
console.log(car); // 'car'
15+
console.log(bike); // 'bike'
16+
console.log(plane); // 'plane'
17+
18+
```
19+
20+
With the destructuring assignment syntax this can be written like this:
21+
22+
23+
```js
24+
const items = ['car', 'bike', 'plane'];
25+
const [car, bike, plane] = items;
26+
27+
console.log(car); // 'car'
28+
console.log(bike); // 'bike'
29+
console.log(plane); // 'plane'
30+
31+
```
32+
33+
This syntax also works with nested arrays:
34+
35+
36+
```js
37+
const items = ['car', ['bike', 'plane'], ['boat']];
38+
const [car, [bike, plane], [boat]] = items;
39+
40+
console.log(car); // 'car'
41+
console.log(bike); // 'bike'
42+
console.log(plane); // 'plane'
43+
console.log(boat); // 'boat'
44+
```
45+
46+
You can combine the destructuring syntax with the rest syntax:
47+
48+
```js
49+
const items = ['car', 'bike', 'plane', 'boat'];
50+
const [car, ...transportation] = items;
51+
52+
console.log(car); // 'car'
53+
console.log(transportation); // ['bike', 'plane', 'boat']
54+
55+
```
56+
57+
### The destructuring assignment syntax with Objects
58+
59+
60+
Destructuring with Objects helps you assigning property values to variables. I only specify the binded property, and this will be bound to the value to of the property.
61+
62+
```js
63+
64+
const car = {
65+
brand: 'ferrari',
66+
type: 'sportscar',
67+
horsepower: 600,
68+
wheels: 4
69+
};
70+
71+
const { brand, type } = car;
72+
73+
console.log(brand); // ferrari
74+
console.log(type); // sportscar
75+
76+
```
77+
78+
Just like with Arrays you can also use this syntax with nested objects:
79+
80+
81+
```js
82+
const car = {
83+
brand: 'ferrari',
84+
type: 'sportscar',
85+
engine: {
86+
horsepower: 600,
87+
liters: 4,
88+
fuel: 'gas'
89+
},
90+
wheels: 4
91+
};
92+
93+
const { engine: { horsepower } } = car;
94+
95+
console.log(horsepower); // 600
96+
97+
```
98+
99+
100+
## Aplication of destructuring
101+
102+
The destructuring syntax can help when accepting parameters in functions. Instead of requesting specific arguments in a specific order it would be a lot easier to just pass one object and then expose only the needed properties using the destructuring syntax:
103+
104+
```js
105+
const carFunction = ({ brand, engine: { horsepower, liters }}) => {
106+
return `${brand} with engine of ${horsepower} horsepower and ${liters} liters`
107+
}
108+
109+
const car = {
110+
brand: 'ferrari',
111+
type: 'sportscar',
112+
engine: {
113+
horsepower: 600,
114+
liters: 4,
115+
fuel: 'gas'
116+
},
117+
wheels: 4
118+
};
119+
120+
console.log(carFunction(car)); // ferrari with engine of 600 horsepower and 4 liters
121+
122+
```
123+
124+
Last but not least the destructuring syntax is very helpful when importing from CommonJS modules. Most likely the module exports more than you need and with this syntax you can avoid cluttering the namespace.
125+
126+
```js
127+
const { session } = require('passport');
128+
session();
129+
130+
// instead of
131+
const passport = require('passport');
132+
passport.session();
133+
```
File renamed without changes.

React/destructuring_in_react-2.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ function Greeting(props) {
4545
function Greeting({ greeting }) {
4646
return <h1>{greeting}</h1>;
4747
}
48-
```
48+
```
49+
50+
### Resources
51+
1> [https://hackernoon.com/understanding-the-destructuring-assignment-syntax-in-javascript-c3bf8e1e908a](https://hackernoon.com/understanding-the-destructuring-assignment-syntax-in-javascript-c3bf8e1e908a)

React/functional-component-declaration-syntax.md

+12
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ const ProtectedRoutes = ({ component: Component, authenticated, ...rest }) => {
6363
Then importing it to another component like below
6464

6565
`import { ProtectedRoutes } from "./ProtectedRoutes";`
66+
67+
#### ES6 doesn't allow export default const. You must declare the constant first then export it:
68+
69+
```js
70+
const Header = () => {
71+
return <pre>Header</pre>;
72+
};
73+
74+
export default Header;
75+
```
76+
77+
This constraint exists to avoid writting export default a, b, c; that is forbidden: only one variable can be exported as default

0 commit comments

Comments
 (0)