๋ฆฌ์กํธ์์ ์๋ฆฌ๋จผํธ์ ์ด๋ฒคํธ๋ฅผ ์ค์ ํด์ค๋์๋ on์ด๋ฒคํธ์ด๋ฆ={์คํํ๊ณ ์ถ์ํจ์} ํํ๋ก ์ค์ ํด์ผ ํฉ๋๋ค.
์ฌ๊ธฐ์ ์ฃผ์ํ์ ์ผ ํ๋ ์ ์, ํจ์ํํ๋ฅผ ๋ฃ์ด์ฃผ์ด์ผ ํ์ง, ํจ์๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์คํํ์๋ฉด ์๋ฉ๋๋ค.
onClick={onIncrease()}
์ด๋ ๊ฒ ํ๋ฉด ๋ ๋๋ง๋๋ ์์ ์์ ํจ์๊ฐ ํธ์ถ๋๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์ ๋๋ค. ์ด๋ฒคํธ๋ฅผ ์ค์ ํ ๋์๋ ํจ์ํ์ ์ ๊ฐ์ ๋ฃ์ด์ฃผ์ด์ผ ํ๋ค๋ ๊ฒ, ์ฃผ์ํด์ฃผ์ธ์.
import React, { useState } from 'react';
์ด ์ฝ๋๋ ๋ฆฌ์กํธ ํจํค์ง์์ useState ๋ผ๋ ํจ์๋ฅผ ๋ถ๋ฌ์์ค๋๋ค.
useState ๋ฅผ ์ฌ์ฉ ํ ๋์๋ ์ํ์ ๊ธฐ๋ณธ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ก ๋ฃ์ด์ ํธ์ถํด์ค๋๋ค. ์ด ํจ์๋ฅผ ํธ์ถํด์ฃผ๋ฉด ๋ฐฐ์ด์ด ๋ฐํ๋๋๋ฐ์, ์ฌ๊ธฐ์ ์ฒซ๋ฒ์งธ ์์๋ ํ์ฌ ์ํ, ๋๋ฒ์งธ ์์๋ Setter ํจ์์
๋๋ค.
๋ฐฐ์ด ๋น๊ตฌ์กฐํ ํ ๋น์ ํตํ์ฌ ๊ฐ ์์๋ฅผ ์ถ์ถํด์ค๋๋ค
const onIncrease = () => {
setNumber(number + 1);
}
const onDecrease = () => {
setNumber(number - 1);
}
๊ฐ์ฒด ์์ 'Getter' ํจ์์ 'Setter' ํจ์๋ฅผ ์ค์ ํ ์ ์๋ค.
'Getter' ํจ์๋ ํจ์ ์์ get์ด๋ผ๋ ํค์๋๋ฅผ, 'Setter' ํจ์๋ ํจ์ ์์ set์ด๋ผ๋ ํค์๋๋ฅผ ๋ถ์ฌ ์ ์ธํ๋ค.
Getter ํจ์๋ ํน์ ๊ฐ์ ์กฐํํ๋ ค๊ณ ํ ๋ ์ฌ์ฉํ๋ค. get ํจ์ ์์ ํน์ ์ฝ๋๋ฅผ ์คํํ๊ณ ์ฐ์ฐ๋ ๊ฐ์ ๋ฐ์ ๋ฆฌํด์ ํ๋ ํ์์ด๋ค. Getter ํจ์๋ ๋ฆฌํด์ด ๋ฐ๋์ ์์ด์ผ ํ๋ค.
const numbers = {
a: 1,
b: 2,
get sum() {
return this.a + this.b;
}
};
console.log(numbers.sum);//3
numbers.b = 5;
console.log(numbers.sum);//6
๋ค์๊ณผ ๊ฐ์ด return ๋๋ ๊ฐ์ด ๋ณํ๋ ๊ฒ์ ์ ์ ์๋ค.
Setter ํจ์๋ set์ด๋ผ๋ ํค์๋๋ฅผ ๋ถ์ฌ ํจ์๋ฅผ ์ ์ธํ๋ค.
Setterํจ์๋ ํ๋ผ๋ฏธํฐ(๋งค๊ฐ๋ณ์)๋ก ๋ฌด์กฐ๊ฑด ์ด๋ค ๊ฐ์ ์ค์ ํด ์ฃผ์ด์ผ ํ๋ค.
const dog = {
_name: "๋ฉ๋ฉ์ด",
set name(value) {
this._name = value;
}
};
console.log(dog._name);//๋ฉ๋ฉ์ด
dog._name = "๋ญ๋ญ์ด";
console.log(dog._name);//๋ญ๋ญ์ด
๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค. Setter ํจ์๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ค์ ํด ์ฃผ์ด์ผ ํ๋ค.
๋๊ฐ์ ์ด๋ฆ์ Getter ํจ์์ Setter ํจ์๋ฅผ ๋ฃ์ด์ค ์ ์๋ค.

Getter ํจ์๋ ํน์ ๊ฐ์ ์กฐํํ ๋ ๋ง๋ค ํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ด๊ณ ,
Setter ํจ์๋ ํน์ ๊ฐ์ ์ค์ ํ ๋๋ง๋ค ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์์ ์ค์ ํ ์ ์๊ณ ์ถ๊ฐ์ ์ผ๋ก ์ฝ๋๋ฅผ ์คํํ ์ ์๋ค.
Getter ํจ์์ Setter ํจ์๋ฅผ ์์๋๋ค๋ฉด ๋์ค์ ํจ์จ์ ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ ๊ฒ์ด๋ค.
Q. Why does the people doing 'Destructuring assignment' in Javascript?
A. Destructuring assignment is a feature in JavaScript that allows you to extract values from arrays or objects and assign them to variables in a more concise and readable way. It can make your code more expressive and easier to understand, especially when dealing with complex data structures.
(๊ตฌ์กฐ๋ถํดํ ๋น์ ๋ฐฐ์ด ๋๋ ๊ฐ์ฒด์์ ๊ฐ์ ์ถ์ถํ์ฌ ๋ณด๋ค ๊ฐ๊ฒฐํ๊ณ ์ฝ๊ธฐ ์ฌ์ด ๋ฐฉ๋ฒ์ผ๋ก ๋ณ์์ ํ ๋นํ ์ ์๋ Javascript์ ๊ธฐ๋ฅ์ด๋ค. ํนํ ๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ๋ค๋ฃฐ ๋ ์ฝ๋๋ฅผ ๋์ฑ ์ดํดํ๊ธฐ ์ฝ๊ณ , ํํํ๊ธฐ ์ฝ๊ฒ ๋ง๋ ๋ค.)
One of the main benefits of destructuring assignment is that it reduces the amount of boilerplate code you need to write. Instead of manually assigning each value from an array or object to a separate variable, you can use destructuring assignment to do it all in one line. This can help make your code more readable and less error-prone.
(๊ตฌ์กฐ๋ถํดํ ๋น์ ์ฃผ์ ์ด์ ์ค ํ๋๋ ์์ฑํด์ผํ๋ ๋ณด์ผ๋ฌ ํ๋ ์ดํธ ์ฝ๋์ ์์ ์ค์ผ ์ ์๋ค๋ ๊ฒ์ด๋ค. ๋ฐฐ์ด ๋๋ ๊ฐ์ฒด์ ๊ฐ์ ๊ฐ๋ณ ๋ณ์์ ์๋์ผ๋ก ํ ๋นํ๋ ๋์ , ๊ตฌ์กฐ๋ถํดํ ๋น์ ์ด์ฉํ์ฌ ๋ชจ๋ ์์
์ ํ ์ค๋ก ์ํํ ์ ์๋ค. ์ด๋ฅผ ํตํด ์ฝ๋๋ฅผ ๋ณด๋ค ์ฝ๊ฒ ์ฝ์ ์ ์๊ณ ์ค๋ฅ ๋ฐ์๋ฅ ์ ์ค์ผ ์ ์๋ค.)
For example, consider the following object:
const person = {
name: 'John',
age: 30,
occupation: 'Engineer'
};
If you want to extract the name and age properties and assign them to variables, you could do it like this:
const name = person.name;
const age = person.age;
With destructuring assignment, you can do it more concisely:
const { name, age } = person;
This assigns the values of person.name and person.age to the variables name and age, respectively.
(person.name๊ฐ -> name์ผ๋ก, person.age๊ฐ age๋ก ํ ๋น๋๋ค)
Destructuring assignment can also be used with arrays, and can even be nested to extract values from nested objects or arrays. Overall, it's a powerful and convenient feature in JavaScript that can make your code more concise and expressive.
(๊ตฌ์กฐ๋ถํดํ ๋น์ ๋ฐฐ์ด๊ณผ ํจ๊ป ์ฌ์ฉํ ์๋ ์์ผ๋ฉฐ ์ค์ฒฉ๋ ๊ฐ์ฒด ๋๋ ๋ฐฐ์ด์์ ๊ฐ์ ์ถ์ถํ๊ธฐ ์ํด ์ค์ฒฉ๋ ์๋ ์๋ค. ์ ์ฒด์ ์ผ๋ก ์ด๊ฒ์ Javascript์ ๊ฐ๋ ฅํ๊ณ ํธ๋ฆฌํ ๊ธฐ๋ฅ์ผ๋ก ์ฝ๋๋ฅผ ๋ณด๋ค ๊ฐ๊ฒฐํ๊ณ ํํ๋ ฅ ์๊ฒ ๋ง๋ ๋ค)
๊ตฌ์กฐ๋ถํดํ ๋น(๋น๊ตฌ์กฐํํ ๋น)์ ์ฌ์ฉํ๋ฉด, ๊ฐ์ฒด ์์ ์๋ ๊ฐ์ ์ถ์ถํ์ฌ ๋ณ์ ํน์ ์์๋ก ๋ฐ๋ก ์ ์ธํด ์ค ์ ์์
chatGPT & ํํ๊ณ ๊ฐ์ฌ
const object = { a: 1, b: 2 };
const { a, b } = object;
console.log(a); // 1
console.log(b); // 2
์ด๋ ๊ฒ๋ง ํ๋ฉด ๋ฏธ๋์ ๋ด๊ฐ ์ดํด๊ฐ ์๊ฐ ๊ฒ ๊ฐ์์ ์ ๋ ์ถ๊ฐ๊ธ
๊ฐ์ฒด ์์
key : value ํํ๋ก ๊ฐ์ด ๋ค์ด๊ฐ ์๋๋ฐ,
์ด๊ฑฐ๋ฅผ ์ด์ ๋ณ์๋ก key๋ฅผ ๊ฐ์ฒด๋ช
์ผ๋ก ์ ์ธ์ ํ๋ฉด
์ ๋ ๊ฒ ๊ตฌ์กฐ๋ถํด๊ฐ ๊ฐ๋ฅํ๋ค~
const object = { a: 1, b: 2 };
function print({ a, b }) {
console.log(a);
console.log(b);
}
print(object);
//1
//2
const object = { a: 1 };
function print({ a, b }) {
console.log(a);
console.log(b);
}
print(object);
// 1
// undefined
๋๋ฒ์งธ ์ถ๋ ฅ์์ undefined๊ฐ ๋ํ๋ ๊ฒ.
์ด๋ฌํ ์ํฉ์ b ๊ฐ์ ๊ธฐ๋ณธ ๊ฐ์ ์ฃผ๊ณ ์ถ๋ค๋ฉด
function print({ a, b = 2 }) {
console.log(a);
console.log(b);
}
ํจ์ ์ ์ธ ํ ๋ ๋งค๊ฐ๋ณ์๋ก ํ ๋นํด๋ฒ๋ฆฌ๊ธฐ
const object = { a: 1 };
const { a, b = 2 } = object;
console.log(a); // 1
console.log(b); // 2
ํจ์ํํ ๋ง๊ณ ๋ ๊ตฌ์กฐ๋ถํด์์๋ ์ด๋ ๊ฒ ํด์ค ์ ์๋ค๋๊ฑฐ~
const animal = {
name: '๋ฉ๋ฉ์ด',
type: '๊ฐ'
};
const nickname = animal.name;
console.log(nickname); // ๋ฉ๋ฉ์ด
์ ์ฝ๋์์๋ animal.name ๊ฐ์ nickname ๊ฐ์ ๋ด๊ณ ์๋๋ฐ์, ์ด๋ฆ์ด ๊ฐ๋ค๋ฉด ๊ทธ๋ฅ ์ฐ๋ฆฌ๊ฐ ์ด์ ์ ๋ฐฐ์ ๋ ๋๋ก ๋น๊ตฌ์กฐํ ํ ๋น์ ์ฐ๋ฉด ๋๋๋ฐ ์ง๊ธ์ ์ด๋ฆ์ด ์๋ก ๋ค๋ฆ ๋๋ค.
const animal = {
name: '๋ฉ๋ฉ์ด',
type: '๊ฐ'
};
const { name: nickname } = animal
console.log(nickname);
์ ์ฝ๋๋ 'animal ๊ฐ์ฒด ์์ ์๋ name ์ nickname ์ด๋ผ๊ณ ์ ์ธํ๊ฒ ๋ค.' ๋ผ๋ ์๋ฏธ์ ๋๋ค.
์ด๊ฑด ์์ง ์ธ๋ชจ๋ฅผ ๋ชจ๋ฅด๊ฒ ๋ค
React ๊ณต์๋ฌธ์ / Hook ์ค๋ช
- React ๊ณต์
Hook์ ์ด์ฉํ์ฌ ๊ธฐ์กด Class ๋ฐํ์ ์ฝ๋๋ฅผ ์์ฑํ ํ์ ์์ด ์ํ๊ฐ๊ณผ ์ฌ๋ฌ React์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ค.
