Framework/React ์™„๋ฒฝ ๊ฐ€์ด๋“œ

[React ์™„๋ฒฝ ๊ฐ€์ด๋“œ] section 2 : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Refresher

yuri lee 2022. 9. 5. 22:29
๋ฐ˜์‘ํ˜•
์ด ๊ธ€์€ udemy์˜ React ์™„๋ฒฝ ๊ฐ€์ด๋“œ with Redux, Next.js, TypeScript ๊ฐ•์ขŒ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

11. ๋ชจ๋“ˆ ์†Œ๊ฐœ

  • If you already know, you can move on to the next section. (for me I have to watch :-( ... )

12. let & const

  • let : ๊ฐ’์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉ
  • const : const๋Š” ํ•œ๋ฒˆ ์ง€์ •ํ•˜๋ฉด ์ ˆ๋Œ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฐ’์ธ ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉ

13. Arrow Functions

function

function printMyname(name) {
  console.log(name);
}

printMyname('Max') // "Max"

function ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ

arrow function

const printMyname = (name) => {
  console.log(name);
}

printMyname('Max') // "Max"

arrow function ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ

const printMyname = name => {
  ...
}

์ธ์ž๊ฐ€ 1๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” () ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

const printMyname =  => { // X
  ...
}

์ „๋‹ฌ ์ธ์ž๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋Š” ()๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์—†๋‹ค.

const multiply = (number) => {
  return number * 2
}
const multiply = number => number *2;

console.log(multiply(2)) //4

๋ฆฌํ„ด๋ฌธ์ธ ๊ฒฝ์šฐ {}์™€ return ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

14. Exports์™€ Imports

default export

import person from './person.js'
import prs from './person.js'
  • import ์‹œ default์— ๋ช…์‹œ๋œ ๊ฐ’์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
  • asํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด alias ์ง€์ •ํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

named export

import {smth} from './utility.js'
import {smth as Smth } from './utility.js'
import * as bundled from './utility.js'
  • import ์‹œ ์ค‘๊ด„ํ˜ธ์— export๋œ ์ƒ์ˆ˜๋ฅผ ์ ์–ด์ค˜์•ผ ํ•œ๋‹ค.
  • ํŒŒ์ผ์— ๋‹ค์ˆ˜์˜ named export๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ํŠน์ˆ˜๋ฌธ์ž *๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ์ผ ๋‚ด ๋ชจ๋“  ๊ฒƒ์„ importํ•  ์ˆ˜ ์žˆ๋‹ค.

15. Classes

class

class Person {
 	name = 'Max' // Property
	call = () => {...} // Method
}
  • ๊ฐ์ฒด๋ฅผ ์œ„ํ•œ ํ•ต์‹ฌ ์ฒญ์‚ฌ์ง„
    • ํ•˜๋‚˜์˜ ๋ชจ๋ธ์ด ๋˜๋Š” ์ฒญ์‚ฌ์ง„(blueprint)๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ์ฒญ์‚ฌ์ง„์„ ๋ฐ”ํƒ•์œผ๋กœ ํ•œ ๊ฐ์ฒด(object)๋ฅผ ๋งŒ๋“œ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจํ„ด์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์ฒญ์‚ฌ์ง„์„ ํด๋ž˜์Šค๋ผ๊ณ  ํ•˜๊ณ  ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šค๋ผ๊ณ  ํ•œ๋‹ค.
  • ํด๋ž˜์Šค๋Š” class ํ‚ค์›Œ๋“œ๋กœ ์ •์˜. ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
    • method : ํด๋ž˜์Šค๋‚˜ ๊ฐ์ฒด์— ์ •์˜ํ•œ ํ•จ์ˆ˜
    • property : ํด๋ž˜์Šค๋‚˜ ๊ฐ์ฒด์— ์ •์˜ํ•œ ๋ณ€์ˆ˜

usage

const myPerson = new Person()
	myPerson.call()
	console.log(myPerson.name)
  • new ํ‚ค์›Œ๋“œ๋กœ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•œ๋‹ค.

Inheritance

class Person extends Master // Inheritance  
  • ํด๋ž˜์Šค์—์„œ ์ƒ์†์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ฆ‰ ๋‹ค๋ฅธ ํด๋ž˜์Šค์— ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์†ํ•˜๋ฉด ์ž ์žฌ์ ์œผ๋กœ ์ƒˆ๋กœ์šด ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. (That's the idea and classes also support inheritance.Which means you have another class which you inherit from taking all its properties and methods and potentially adding new properties and methods)

Example

class Human {
  constructor() {
    this.gender = 'male'
  }
  printGender() {
    console.log(this.gender);
  }
}

class Person extends Human {
  constructor() {
    super();
    this.name = 'Max'
  }
  
  printMyName() {
    console.log(this.name);
  }
}

const person = new Person();
person.printMyName(); // "Max"
person.printGender(); // "male"
  • extends : ๋ฅผ ์ด์šฉํ•˜์—ฌ Human ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ํ™•์žฅํ•˜์˜€๋‹ค.
  • super ์ƒ์„ฑ์ž : ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•˜๊ณ  ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์•ˆ์— super ๋ฉ”์†Œ๋“œ๋ฅผ ํ•„์ˆ˜์ ์œผ๋กœ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ super๋ฅผ ๋ช…์‹œํ•ด์ฃผ์ง€ ์•Š์„ ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
class Person extends Human {
  constructor() {
    super();
    this.name = 'Max'
    this.gender = 'femal'
  }
}

const person = new Person();
person.printMyName(); // "Max"
person.printGender(); // "femal"

 this.gender = 'femal' ์ถ”๊ฐ€์‹œ "femal" ์ด ์ถœ๋ ฅ๋œ๋‹ค.

ํด๋ž˜์Šค๊ฐ€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋ฅผ ์œ„ํ•œ ์ฒญ์‚ฌ์ง„์ด๋ผ๋Š” ๊ฒƒ์„ ๊ผญ ์ดํ•ดํ•ด์•ผ ํ•˜๋ฉฐ, ํด๋ž˜์Šค๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ๋น„์Šทํ•˜๊ณ  ์ƒ์†์€ ํ”„๋กœํ† ํƒ€์ž…๊ณผ ๋น„์Šทํ•˜๋‹ค.

16. Classes, Properties & Methods

myProperty = 'value';
myMethod = () => {...}
  • ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์œผ๋กœ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • this ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
  • ์ค‘์š”) ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ES6/Babel ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

Example

class Human {
  gender = 'male'
  printGender = () => {
    console.log(this.gender);
  }
}

class Person extends Human {
    name = 'Max'
    gender = 'femal';

    printMyName = () => {
      console.log(this.name);
    }
}

const person = new Person();
person.printMyName(); // "Max"
person.printGender(); // "femal"

17. Spread & Rest Operators (...)

  • ์  3๊ฐœ(...)๋กœ ์ด๋ฃจ์–ด์ง„ ์—ฐ์‚ฐ์ž๋กœ, ์‚ฌ์šฉ์šฉ๋„์— ๋”ฐ๋ผ ๊ตฌ๋ถ„๋œ๋‹ค.

Spread

  • Used to split up array elements OR object properties
const newArray = [...oldArray, 1, 2]
const newObject = {...oldObject, newProp:5}

Rest

  • Used to merge a list of function arguments into an array
function sortArgs(...args) {
	return args.sort()
}

Example

# Spread
const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4]
const newNumbers2 = [numbers, 4]

console.log(newNumbers); // [1, 2, 3, 4]
console.log(newNumbers2); // [1, 2, 3, 4]


const person = {
  name : 'Max'
}

const newPerson = {
  ...person,
  age: 28
}

console.log(newPerson); // [object Object] { age: 28, name: "Max"}

# Rest

const filter = (...args) => {
  return args.filter(el => el === 1);
}
console.log(filter(1,2,3,1)) // [1, 1]

→ return ํ‚ค์›Œ๋“œ ์—†์ด ๊ตฌ๋ฌธ์„ ํ•œ์ค„๋กœ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์Œ.

18. Destructuring (๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น)

// ๋ฐฐ์—ด์˜ destructuring => ๋ณ€์ˆ˜ a, b์— ๊ฐ๊ฐ์˜ ๊ฐ’์„ ํ• ๋‹น
[a, b] = ['Hello', 'Test'];
console.log(a); // Hello
console.log(b); // Test

// ๊ฐ์ฒด์˜ destructuring => ๊ฐ์ฒด๊ฐ„ ๋™์ผํ•œ key๋ฅผ ๊ฐ€์ง„ ๊ฐ’์„ ๋Œ€์ƒ์œผ๋กœ ํ•จ
{name} = { name:'Test', age:27 };
console.log(name); // Test
console.log(age); // undefined
  • Easily extract array elements or object properties and store them in variables
  • Spread & Rest Operators์™€ ๋น„์Šทํ•ด ๋ณด์ด์ง€๋งŒ ๋‹ค๋ฆ„!
    • Spread : ๋ชจ๋“  ์›์†Œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ ธ์™€ ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์— ์ „๋‹ฌ
    • Destructuring : ์›์†Œ ํ˜น์€ ํ”„๋กœํผํ‹ฐ ํ•˜๋‚˜๋งŒ ๊ฐ€์ ธ์™€ ๋ณ€์ˆ˜์— ์ €์žฅ

example

19. reference & primitive types

๊ฐ’ ๋ณต์‚ฌ

const number = 1;
const number2 = number;
console.log(number2); // 1

๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ํ• ๋‹น ์‹œ ์ฃผ์†Œ๊ฐ’์ธ ํฌ์ธํ„ฐ๋ฅผ ๋ณต์‚ฌ


→ secondPerson ์ถœ๋ ฅํ•ด๋ณด๋ฉด secondPerson ๊ฐ’๋„ Manu๋กœ ๋ฐ”๋€Œ์–ด ์žˆ๋‹ค.
→ ๋ฆฌ์•กํŠธ์—์„œ ๋งŽ์ด ํ•˜๋Š” ์‹ค์ˆ˜ ์ค‘ ํ•˜๋‚˜๋กœ ์ด๋Ÿด ๋•Œ๋Š” Spread ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Spread์˜ ํ™œ์šฉ


→ secondPerson์„ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ๊ธฐ์กด์— ํ• ๋‹น๋˜์—ˆ๋˜ 'Max' ๊ฐ’์ด ์œ ์ง€๋˜๊ณ  ์žˆ๋‹ค.

20. Array Functions

์ค‘์š”ํ•œ ๋ฐฐ์—ด ํ•จ์ˆ˜

  • filter()
  • map()
  • sort()

Example

const numbers = [1,2,3]

const doubleNumArray = numbers.map((num) => {
  return num * 2;
})

console.log(numbers); // [1, 2, 3]
console.log(doubleNumArray) // [2, 4, 6]

21. ๋งˆ๋ฌด๋ฆฌ A Refresher

  • ์ฐจ์„ธ๋Œ€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ์ค‘์š”ํ•œ ๋ช‡๋ช‡ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ „๋ฐ˜์ ์œผ๋กœ ์‚ดํŽด๋ณด์•˜๋‹ค.

22. ์ฐจ์„ธ๋Œ€ JavaScript - ์š”์•ฝ

  • 11 ~ 22 ๋‚ด์šฉ

23. JS Array functions

JS Array functions ์ƒฌํŽด๋ณด๊ธฐ

๐ŸคŸ ๋Š๋‚€์ 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐœ๋…์„ ํ•œ๋ฒˆ ๋” ์ „์ฒด์ ์œผ๋กœ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์•˜๋‹ค.  ์˜์–ด ๊ฐ•์˜๋กœ ๋“ค์–ด๋ณด๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ์ƒ๊ฐ๋ณด๋‹ค ์‰ฝ์ง€ ์•Š๋„ค.. ใ… ใ…  ๊ณ„์† ํ•ด์„๋ณธ์„ ์ฐพ์•„๋ณด๊ฒŒ ๋œ๋‹ค... ์•ž์œผ๋กœ ๋” ์ฝ”๋“œ์™€ ์˜์–ด์— ์ต์ˆ™ํ•ด์ง€๋„๋ก ํ•˜์ž. ํ™”์ดํŒ…!~~

 


https://www.udemy.com/course/best-react/
https://velog.io/@smj0818/%EA%B0%9D%EC%B2%B4%EC%A7%80%ED%96%A5-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8

๋ฐ˜์‘ํ˜•