ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

์ด ๊ธ€์€ ๋ˆ„๊ตฌ๋“ ์ง€ ํ•˜๋Š” ๋ฆฌ์•กํŠธ [www.youtube.com/playlist?list=PL9FpF_z-xR_E4rxYMMZx5cOpwaiwCzWUH]์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

 

ํ”„๋ก ํŠธ์•ค๋“œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ : React, Angular, Vue ๋“ฑ

MVC, MVVM, MVW ๋“ฑ์„ ์‚ฌ์šฉํ•˜๋˜ ๊ธฐ์กด ์›น ํ”„๋ ˆ์ž„์›Œํฌ/๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

๊ณตํ†ต์  : Model (์–‘๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ)

๋ณ€ํ™”(Mutation)

๊ทธ๋ƒฅ mutation์„ ํ•˜์ง€ ๋ง๊ณ , ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋ทฐ๋ฅผ ๋‚ ๋ ค๋ฒ„๋ฆฌ๊ณ  ์ƒˆ๋กœ ๋งŒ๋“ค์ž!

Virtual DOM : ๊ฐ€์ƒ์˜ ๋”์— ๋ Œ๋”๋ง์„ ํ•˜๊ณ  ๊ธฐ์กด์˜ ๋”์„ ๋น„๊ตํ•œ ํ›„, ์ •๋ง ๋ณ€ํ™”๊ฐ€ ํ•„์š”ํ•œ ๊ณณ์—๋งŒ ์—…๋ฐ์ดํŠธ ํ•ด์คŒ

Virtual DOM

Webpack

์ฝ”๋“œ๋“ค์„ ์˜์กดํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ž˜ ํ•ฉ์ณ์„œ ํ•˜๋‚˜ ๋˜๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ํŒŒ์ผ๋กœ ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ค์–ด๋ƒ„

Webpack

-> 'bundling์ž‘์—…' ์ด๋ผ๊ณ  ํ•จ

 

Babel : JS ๋ณ€ํ™˜ ๋„๊ตฌ

 

 

#8 Props

Props ์‚ฌ์šฉ - ์ž์‹ ์ปดํฌ๋„ŒํŠธ

{this.props.name}

 

Props ์‚ฌ์šฉ - ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ

<MyName name="๋ฆฌ์•กํŠธ" />;

MyName์€ ํด๋ž˜์Šค๋ช…์ด๊ณ , ๋’ค์˜ name="๋ฆฌ์•กํŠธ" ๊ฐ€ props์ด๋‹ค.

 

 

๋””ํดํŠธ ๊ฐ’ ์„ค์ • ๋ฐฉ๋ฒ•1 - ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ (์ตœ์‹ ์‹)

  static defaultProps = {
    name: '๊ธฐ๋ณธ์ด๋ฆ„'
  };

 

๋””ํดํŠธ ๊ฐ’ ์„ค์ • ๋ฐฉ๋ฒ•2 - ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ

MyName.defaultProps = {
  name: '๊ธฐ๋ณธ์ด๋ฆ„'
};

 

<MyName.js>

import React, { Component } from 'react';

class MyName extends Component {
  static defaultProps = {
    name: '๊ธฐ๋ณธ์ด๋ฆ„'
  };
  render() {
    return (
      <div>
        ์•ˆ๋…•ํ•˜์„ธ์š” ์ œ ์ด๋ฆ„์€ <b>{this.props.name}</b>์ž…๋‹ˆ๋‹ค.
      </div>
    );
  }
}

export default MyName;

 

<App.js>

import React, { Component } from 'react';
import MyName from './MyName';

class App extends Component {
  render() {
    return <MyName name="๋ฆฌ์•กํŠธ" />;
  }
}

export default App;

 

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ

์˜ˆ์‹œ) MyName.js

import React from 'react'; //{ Component } ์‚ญ์ œ

const MyName = ({ name }) => { // ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น
  return <div>์•ˆ๋…•ํ•˜์„ธ์š” ์ œ ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค.</div>;
};

MyName.defaultProps = {
  name: '๊ธฐ๋ณธ์ด๋ฆ„'
};
export default MyName;

 

 

 

 

#9 State

 

state๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ณ€๊ฒฝ์‹œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋Š” setState()์ด๋‹ค.

state๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค rerendering์„ ํ•œ๋‹ค.

(rendering์ด๋ž€? render() ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ๋œปํ•จ)

 

**props VS State**

  props State
๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๋ฐฉํ–ฅ ๋ถ€๋ชจ -> ์ž์‹ ์ž๊ธฐ ์ž์‹ 
์ˆ˜์ • ๊ฐ€๋Šฅ ์—ฌ๋ถ€ ์ฝ๊ธฐ์ „์šฉ(๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ) ๋‚ด๋ถ€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ

 

<Counter.js>

import React, { Component } from 'react';

class Counter extends Component {
  state = {
    number: 0
  };

  handleIncrease = () => {
    this.setState({
      number: this.state.number + 1
    });
  };

  handleDecrease = () => {
    this.setState({
      number: this.state.number - 1
    });
  };

  render() {
    return (
      <div>
        <h1>์นด์šดํ„ฐ</h1>
        <div>๊ฐ’: {this.state.number}</div>
        <button onClick={this.handleIncrease}>+</button>
        <button onClick={this.handleDecrease}>-</button>
      </div>
    );
  }
}

export default Counter;

 

<App.js>

import React, { Component } from 'react';
import Counter from './Counter';

class App extends Component {
  render() {
    return <Counter />;
  }
}

export default App;

 

 

#10 LifeCycle API

 

๋ผ์ดํ”„์‚ฌ์ดํด ์ข…๋ฅ˜

1. ๋‚˜ํƒ€๋‚  ๋•Œ

2. ์—…๋ฐ์ดํŠธ ๋  ๋•Œ

  โ‘  props๊ฐ€ ๋ฐ”๋€” ๋•Œ

  โ‘ก state๊ฐ€ ๋ฐ”๋€” ๋•Œ

  โ‘ข ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง๋  ๋•Œ

  โ‘ฃ this.forceUpdate๋กœ ๊ฐ•์ œ๋กœ ๋ Œ๋”๋ง์„ ํŠธ๋ฆฌ๊ฑฐํ•  ๋•Œ

3. ์‚ฌ๋ผ์งˆ ๋•Œ

 

์•”๊ธฐX. ์•Œ๊ณ  ์žˆ๋‹ค๊ฐ€ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์— ์“ฐ๋ฉด ๋œ๋‹ค.

 

https://twitter.com/dan_abramov/status/981712092611989509

 

Mounting : ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ธŒ๋ผ์šฐ์ € ์ƒ์— ๋‚˜ํƒ€๋‚  ๋•Œ

Updating : ์ปดํฌ๋„ŒํŠธ์˜ props๋‚˜ state๊ฐ€ ๋ฐ”๋€Œ์—ˆ์„ ๋•Œ

Unmounting : ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ธŒ๋ผ์šฐ์ € ์ƒ์— ์‚ฌ๋ผ์งˆ ๋•Œ

 

  • constructor : ์ƒ์„ฑ์ž ํ•จ์ˆ˜ - ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด ์งˆ ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜, state ์ดˆ๊ธฐํ™” ๋“ฑ
  • getDerivedStateFromProps : props๋กœ ๋ฐ›์€ ๊ฐ’์„ state์— ๋™๊ธฐํ™”์‹œํ‚ค๊ณ  ์‹ถ์„ ๋•Œ
  • render : ์–ด๋–ค ๋”์„ ๋งŒ๋“ค์ง€, ๋‚ด๋ถ€ ํƒœ๊ทธ์— ์–ด๋–ค ๊ฐ’์„ ์ „๋‹ฌํ•ด ์ค„์ง€
  • React updates Dom and refs
  • componentDidMount : ์‹ค์ œ๋กœ ๋ธŒ๋ผ์šฐ์ €์— ๋‚˜ํƒ€๋‚  ๋•Œ/ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(์ฐจํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋“ฑ) ์‚ฌ์šฉ ์‹œ ํŠน์ • ๋”์— ์ฐจํŠธ๋ฅผ ๊ทธ๋ ค๋‹ฌ๋ผ๋Š” ์‹์˜ ์ฝ”๋“œ ์ž‘์„ฑ/ ๋„คํฌ์›Œํฌ, api, ajax ์š”์ฒญ/ ์ปดํฌ๋„ŒํŠธ ๋‚˜ํƒ€๋‚œ ๋’ค ๋ช‡์ดˆ ๋’ค ์ด๋ฒคํŠธ(์Šคํฌ๋กค, ํด๋ฆญ ๋“ฑ) ํ•˜๊ณ ์‹ถ์„ ๋•Œ, ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋‹ ๋“ฑ
  • shouldComponentUpdate : ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋˜๋Š” ์„ฑ๋Šฅ์„ ์ตœ์ ํ™” ์‹œํ‚ค๊ณ  ์‹ถ์„ ๋•Œ, virtual dom์—๋„ ๋žœ๋”๋ง ํ• ์ง€ ๋ง์ง€ ๊ฒฐ์ •(true์ผ ๋•Œ ๋ฐ‘์— ๊ฒƒ๋“ค ํ˜ธ์ถœ, false๋ฉด ํ˜ธ์ถœX)
  • getSnapshotBeforeUpdate : ๋ Œ๋”๋ง ํ•œ ๊ฒฐ๊ณผ๋ฌผ์ด ๋ธŒ๋ผ์šฐ์ €์— ๋ฐ˜์˜๋˜๊ธฐ ๋ฐ”๋กœ ์ง์ „์— ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
  • componentDidUpdate : ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
  • componentWillUmmount : componentDidMount์—์„œ ์„ค์ •ํ•œ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์—†์•จ ๋•Œ
  • * componentDidCatch : ๋ฒ„๊ทธ์žก์„ ๋•Œ

 

๋Œ“๊ธ€