๐ [JavaScript] React ์์์ ๋ฌธ๋ฒ์ ๋ฆฌ
![๐ [JavaScript] React ์์์ ๋ฌธ๋ฒ์ ๋ฆฌ](/assets/img/thumbnail/b1-thumbnail.jpg)
[JavaScript] React ์์์ ๋ฌธ๋ฒ์ ๋ฆฌ
์ด ๋ชจ๋์์, ์ ๋ ๋ช๋ช ํต์ฌ ์ฐจ์ธ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ๋ฅ๋ค์ ๋ํ ๊ฐ๋ตํ ์๊ฐ๋ฅผ ํด ๋๋ ธ์ต๋๋ค. ๋ฌผ๋ก ์ด ๊ณผ์ ์์ ์ฌ๋ฌ๋ถ๋ค์ด ์์ฃผ ๋ณด์๊ฒ ๋ ๊ฒ๋ค์ ์ด์ ์ ๋ง์ถ์์ฃ . ์ฌ๊ธฐ ๊ฐ๋ตํ ์์ฝ์ด ์์ต๋๋ค!
let & const
let ์ ๋ํด ๋ ์ฝ์ด๋ณด๊ธฐ: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
const์ ๋ํด ๋ ์ฝ์ด๋ณด๊ธฐ:: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
let ๊ณผ const ๋ ๊ธฐ๋ณธ์ ์ผ๋ก var ๋ฅผ ๋์ฒดํฉ๋๋ค. ์ฌ๋ฌ๋ถ์ var ๋์ let ์ ์ฌ์ฉํ๊ณ , var ๋์ const๋ฅผ ์ฌ์ฉํ๊ฒ ๋ฉ๋๋ค. ๋ง์ฝ ์ด ๋ณ์๋ฅผ ๋ค์ ํ ๋นํ์ง ์์ ๊ฒฝ์ฐ์ ๋ง์ด์ฃ (๋ฐ๋ผ์ ํจ๊ณผ์ ์ผ๋ก constant๋ก ๋ณํํฉ๋๋ค).
ES6 Arrow Functions ๋ ์ฝ์ด๋ณด๊ธฐ: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Arrow function์ JavaScript ํ๊ฒฝ์์ํจ์๋ฅผ ์์ฑํ๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ๋๋ค. ๋ ์งง์ ๊ตฌ๋ฌธ ์ธ์๋ this ํค์๋์ ๋ฒ์๋ฅผ ์ ์งํ๋๋ฐ ์ ์ด์ ์ ์ ๊ณตํฉ๋๋ค (์ฌ๊ธฐ๋ฅผ ๋ณด์ธ์).
Arrow function ๊ตฌ๋ฌธ์ ๋ฏ์ค๊ฒ ๋ณด์ผ ์ ์์ผ๋ ์ฌ์ค ๊ฐ๋จํฉ๋๋ค.
function callMe(name) {
console.log(name);
}
๋ํ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์๋ ์์ต๋๋ค:
const callMe = function(name) {
console.log(name);
}
์ด๋ ๊ฒ ๋ฉ๋๋ค:
const callMe = (name) => {
console.log(name);
}
์ค์:
arguments๊ฐ ์๋ ๊ฒฝ์ฐ, ํจ์ ์ ์ธ์ ๋น ๊ดํธ๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค:
const callMe = () => {
console.log('Max!');
}
์ ํํ ํ๋์ argument๊ฐ ์๋ ๊ฒฝ์ฐ, ๊ดํธ๋ฅผ ์๋ตํ ์ ์์ต๋๋ค:
const callMe = name => {
console.log(name);
}
value๋ฅผ returnํ ๋, ๋ค์๊ณผ ๊ฐ์ ์์ปท์ ์ฌ์ฉํ ์ ์์ต๋๋ค:
const returnMe = name => name
์ด๊ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
const returnMe = name => {
return name;
}
Exports & Imports
React ํ๋ก์ ํธ์์ (๊ทธ๋ฆฌ๊ณ ์ค์ ๋ก ๋ชจ๋ ์ต์ JavaScript
์์), ๋ชจ๋์ด๋ผ ๋ถ๋ฆฌ๋ ์ฌ๋ฌ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ๋ค์ ์ฝ๋๋ฅผ ๋ถํ ํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๊ฐ file/ ๋ชจ๋์ ๋ชฉ์ ์ ๋ช
ํํ๊ฒ ํ๊ณ ๊ด๋ฆฌ๊ฐ ์ฉ์ดํ๊ฒ ํฉ๋๋ค.
๋ค๋ฅธ ํ์ผ์ ๊ธฐ๋ฅ์ ๊ณ์ ์ก์ธ์คํ๋ ค๋ฉด export
(availableํ๊ฒ ํ๊ธฐ ์ํด) ๋ฐ import
์์ธ์ค๋ฅผ ํ๋ณดํ๊ธฐ ์ํด) statements
๊ฐ ํ์ํฉ๋๋ค.
๋ ๊ฐ์ง ์ ํ์ export
๊ฐ ์์ต๋๋ค: default
(unnamed)์ named
์
๋๋ค.
default => export default ...;
named => export const someData = ...;
default exports๋ฅผ ๋ค์๊ณผ ๊ฐ์ด import
ํ ์ ์์ต๋๋ค.
import someNameOfYourChoice from './path/to/file.js';
๋๋๊ฒ๋, someNameOfYourChoice ์ ์ ์ผ๋ก ์ฌ๋ฌ๋ถ์๊ฒ ๋ฌ๋ ค ์์ต๋๋ค.
Named exports๋ ์ด๋ฆ์ผ๋ก import๋์ด์ผ ํฉ๋๋ค:
import { someData } from './path/to/file.js';
ํ์ผ ํ๋๋ ์ค์ง ํ๋์ default์ ๋ฌดํํ named exports
๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค. ํ๋์ default๋ฅผ ๊ฐ์ ํ์ผ ๋ด์์ named exports
์ ๋ฏน์คํ ์ ์์ต๋๋ค.
named exports
๋ฅผ import
ํ ๋, ๋ค์ ๊ตฌ๋ฌธ์ ์ด์ฉํด ํ ๋ฒ์ ๋ชจ๋ named exports
๋ฅผ import
ํ ์ ์์ต๋๋ค.
import * as upToYou from './path/to/file.js';
upToYou ๋ ๋ชจ๋ exported ๋ณ์/ํจ์๋ฅผ ํ๋์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด์ ๋ชจ์๋๋ค. ์๋ฅผ ๋ค์ด, export const someData = ... (/path/to/file.js )
์ด์ ๊ฐ์ด upToYou ์ ์ก์ธ์ค ํ ์ ์์ต๋๋ค: upToYou.someData .
Classes
Classes๋ constructor ํจ์์ prototypes๋ฅผ ๋์ฒดํ๋ ๊ธฐ๋ฅ์ ๋๋ค. ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด์ blueprints๋ฅผ ์ ์ํ ์ ์์ต๋๋ค.
์์:
class Person {
constructor () {
this.name = 'Max';
}
}
const person = new Person();
console.log(person.name); // prints 'Max'
์์ ์์์์, class๋ฟ ๋ง ์๋๋ผ ํด๋น class์ property (=> name) ์ด ์ ์๋ฉ๋๋ค. ํด๋น ๊ตฌ๋ฌธ์, property๋ฅผ ์ ์ํ๋ โ๊ตฌ์โ ๊ตฌ๋ฌธ์
๋๋ค. ์ต์ ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก์ ํธ์์๋ (์ด ์ฝ์ค์์ ์ฌ์ฉ๋ ๊ฒ์ฒ๋ผ), ๋ค์๊ณผ ๊ฐ์ ๋ณด๋ค ํธ๋ฆฌํ ์ ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด class property๋ฅผ ์ ์ํฉ๋๋ค:
class Person {
name = 'Max';
}
const person = new Person();
console.log(person.name); // prints 'Max'
๋ฉ์๋๋ฅผ ์ ์ํ ์๋ ์์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ด ๋ง์ด์ฃ :
class Person {
name = 'Max';
printMyName () {
console.log(this.name); // this is required to refer to the class!
}
}
const person = new Person();
person.printMyName();
ํน์ ์ด์ ๊ฐ์ด ํ ์๋ ์์ต๋๋ค:
class Person {
name = 'Max';
printMyName = () => {
console.log(this.name);
}
}
const person = new Person();
person.printMyName();
๋ ๋ฒ์งธ ์ ๊ทผ ๋ฐฉ์์ all arrow function๊ณผ ๊ฐ์ ์ด์ ์ด ์์ต๋๋ค: thisํค์๋๊ฐ reference๋ฅผ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค.
class ์ฌ์ฉ์ inheritance๋ฅผ ์ฌ์ฉํ ์๋ ์์ต๋๋ค.
class Human {
species = 'human';
}
class Person extends Human {
name = 'Max';
printMyName = () => {
console.log(this.name);
}
}
const person = new Person();
person.printMyName();
console.log(person.species); // prints 'human'
Spread & Rest Operator
Spread ์ rest operator๋ ์ฌ์ค ๊ฐ์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํฉ๋๋ค: โฆ
๋ง์ต๋๋ค, ์ฐ์ฐ์์ ๋๋ค - ์ ์ธ๊ฐ์ฃ . ์ด๊ฒ์ ์ฌ์ฉํด spread๋ก ์ฌ์ฉํ ์ง rest operator๋ก ์ฌ์ฉํ ์ง ๊ฒฐ์ ํฉ๋๋ค.
Spread Operator ์ฌ์ฉํ๊ธฐ:
Spread operator๋ ๋ฐฐ์ด์์ ์์๋ค์ ๊ฐ์ ธ์ค๊ฑฐ๋ (=> ๋ฐฐ์ด์ ์์๋ค์ ๋ฆฌ์คํธ๋ก ๋ถํด) ๊ฐ์ฒด์์ ์์ฑ์ ๊ฐ์ ธ์ต๋๋ค.
๋ ๊ฐ์ง ์์๊ฐ ์์ต๋๋ค:
const oldArray = [1, 2, 3];
const newArray = [...oldArray, 4, 5]; // This now is [1, 2, 3, 4, 5];
๊ฐ์ฒด์ spread operator๋ฅผ ์ฌ์ฉํ ์์์ ๋๋ค:
const oldObject = {
name: 'Max'
};
const newObject = {
...oldObject,
age: 28
};
๊ทธ๋ฌ๋ฉด newObject๋ ๋ค์์ด ๋ ๊ฒ์ ๋๋ค.
{
name: 'Max',
age: 28
}
sperad operator๋ ๋ฐฐ์ด๊ณผ ๊ฐ์ฒด๋ฅผ ๋ณต์ ํ๋๋ฐ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ๋ ๋ค (primitives๊ฐ ์๋) reference ์ ํ์ด๊ธฐ ๋๋ฌธ์, ์์ ์ ์ผ๋ก ๋ณต์ฌ๋ฅผ ํ๋๊ฒ ์ด๋ ค์ธ ์ ์์ต๋๋ค. (๋ณต์ฌ๋ ์๋ณธ์ future mutation ๋ฐ์ ๋ฐฉ์ง). Spread operator๋ก, ๊ฐ์ฒด๋ ๋ฐฐ์ด์ ๋ณต์ฌ๋ณธ (shallow!)์ ์ฝ๊ฒ ์ป์ ์ ์์ต๋๋ค.
Destructuring
Destructuring์ ์ฌ์ฉํ๋ฉด ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด์ ๊ฐ์ ์ฝ๊ฒ ์์ธ์คํ ์ ์๊ณ ๋ณ์์ ํ ๋นํ ์ ์์ต๋๋ค.
ํ ๋ฐฐ์ด์ ์์์ ๋๋ค:
const array = [1, 2, 3];
const [a, b] = array;
console.log(a); // prints 1
console.log(b); // prints 2
console.log(array); // prints [1, 2, 3]
๋ค์์ ๊ฐ์ฒด์ ์์์
๋๋ค:
const myObj = {
name: 'Max',
age: 28
}
const {name} = myObj;
console.log(name); // prints 'Max'
console.log(age); // prints undefined
console.log(myObj); // prints {name: 'Max', age: 28}
Destructuring์ ์ธ์๋ฅผ ๊ฐ์ง ํจ์๋ฅผ ์์ ํ ๋ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ์ด ์์๋ฅผ ๋ณด์์ฃ :
const printName = (personObj) => {
console.log(personObj.name);
}
printName({name: 'Max', age: 28}); // prints 'Max'
์ฌ๊ธฐ์, ํจ์๋ด name๋ง์ printํ๊ณ ์ถ์ง๋ง ํจ์์ ์์ ํ person ๊ฐ์ฒด๋ฅผ ๋ณด๋ด๊ณ ์์ต๋๋ค. ๋น์ฐํ ์ด๊ฒ์ ๋ฌธ์ ๊ฐ ๋์ง ์์ง๋ง personObj.name์ ์ด ํจ์๋ด์์ ํธ์ถํด์ผ๋ง ํฉ๋๋ค. ์ด ์ฝ๋๋ฅผ destructuring์ผ๋ก ์์ถ์์ผ ๋ณด๊ฒ ์ต๋๋ค.
const printName = ({name}) => {
console.log(name);
}
printName({name: 'Max', age: 28}); // prints 'Max')
์์ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ง๋ง ์ฝ๋๊ฐ ์ค์์ต๋๋ค. Destructuring์ ํตํด, name property๋ฅผ ๊ฐ์ ธ์ name ์ด๋ผ๋ ์ด๋ฆ์ ๋ณ์/์ธ์์ ์ ์ฅํ๊ณ ํจ์ ๋ณธ๋ฌธ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
javascript ์ต๋ช ํจ์ ์ ์ธ
function(){ ... }
() => { ... }
(userName, userAge) => { ... }
userName => { ... }
number => number * 3;
var : ์ค๋ณต ์ ์ธ๊ณผ ์ฌํ ๋น ๊ฐ๋ฅ, ํจ์ ๋จ์ ์ค์ฝํ, ํธ์ด์คํ undefined์ผ๋ก ์ด๊ธฐํ let : ์ค๋ณต์ ์ธ ๋ถ๊ฐ๋ฅ ์ฌํ ๋น ๊ฐ๋ฅ, ๋ธ๋ก ๋จ์ ์ค์ฝํ, ํธ์ด์คํ Reference Error๊ฐ ๋ฐ์, ๋ณ์์ ์ ์ธ๊ณผ ์ด๊ธฐํ ์ฌ์ด์ ์ผ์์ ์ผ๋ก ๋ณ์๊ฐ์ ์ฐธ์กฐํ ์ ์๋ ๊ตฌ๊ฐ์ธ TDZ(Temporal Dead Zone)๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ const : ์ค๋ณต์ ์ธ ์ฌํ ๋น ๋ถ๊ฐ๋ฅ(๋จ ์์ฑ๊ฐ์ ๊ฐ๋ฅ), ๋ธ๋ก ๋จ์ ์ค์ฝํ, ํธ์ด์คํ let๊ณผ ๋์ผ
๋ณ์ ์ ์ธ์๋ ๊ธฐ๋ณธ์ ์ผ๋ก const๋ฅผ ์ฌ์ฉํ๊ณ , ์ฌํ ๋น์ด ํ์ํ ๊ฒฝ์ฐ์ ํ์ ํด let ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ์ฒด๋ฅผ ์ฌํ ๋นํ๋ ๊ฒฝ์ฐ๋ ์๊ฐ๋ณด๋ค ํํ์ง ์๋ค. const ๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์น ์์ ์ฌํ ๋น์ ๋ฐฉ์งํด ์ฃผ๊ธฐ ๋๋ฌธ์ ๋ณด๋ค ์์ ํ๋ค. ์ฌํ ๋น์ด ํ์ํ ๊ฒฝ์ฐ์ ํ์ ํด let ์ ์ฌ์ฉํ๋ค. ์ด๋, ๋ณ์์ ์ค์ฝํ๋ ์ต๋ํ ์ข๊ฒ ๋ง๋ ๋ค. ์ฌํ ๋น์ด ํ์ ์๋ ์์์ ๊ฐ์ฒด์๋ const ๋ฅผ ์ฌ์ฉํ๋ค.
myVar=3
console.log(myVar); // 3
var myVar = 5;
console.log(myVar); // 5
console.log(youVar) // undefind
var youVar = 3
console.log(myLet); // ReferenceError
let myLet = 10;
console.log(myLet); // ReferenceError
const youLet = 10;
์ถ๊ฐ ์ ๋ณด๋ ์๋ ๋งํฌ ์ฐธ๊ณ
- [[๋ณ์ ์ ์ธ, ์ด๊ธฐํ, ํ ๋น & ํธ์ด์คํ (Hoisting)]]
- [[JS - ํธ์ด์คํ (Hoisting)]]
- [[์ ์ธํ, ๋ช ๋ นํ ์ฝ๋ ๊ทธ๋ฆฌ๊ณ ์ถ์ํ]]
JS Array functions
ํ์ดํ ํจ์ ์ฌ์ฉ๋ฒ์ ๋งํฌ ์ฐธ์กฐ
map()
ย ย =>ย https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/mapfind()
ย ย =>ย https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findfindIndex()
ย ย =>ย https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndexfilter()
ย ย =>ย https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filterreduce()
ย ย =>ย https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce?v=bconcat()
ย ย =>ย https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat?v=bslice()
ย ย =>ย https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slicesplice()
ย ย =>ย https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
๐ Reference
- ใํ๊ธ์๋งใ React ์๋ฒฝ ๊ฐ์ด๋ 2025 with React Router & Redux | Udemy
- ์ด๋ฏธ์ง ์ถ์ Freepik | ์ฌ์ธ์ AI ํฌ๋ฆฌ์์ดํฐ๋ธ ํด