Liam
Liam
I'm studying ๐Ÿฅธ

React

React



๐Ÿ’ก Intro

๋ฐฑ์•ค๋“œ ๊ฐœ๋ฐœ์„ ํ•˜๋ฉด์„œ ๊ฐ„๋‹จํ•œ Admin ์‚ฌ์ดํŠธ๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ๋Š”๋ฐ, ๋‹จ์ˆœ javascript๋กœ ๊ฐœ๋ฐœํ•œ ์–ด๋“œ๋ฏผ์€ ์ƒ๊ฐ๋ณด๋‹ค ๋งŽ์€ ๋ฆฌ์†Œ์Šค๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค๋Š” ๋Š๋‚Œ์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์‚ฌ์šฉ์ž์—๊ฒŒ๋Š” ์กฐ๊ธˆ ๋” ๋‚˜์€ ๊ฒฝํ—˜์„ ์ค„ ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ฐœ์ž์—๊ฒŒ๋„ ๋” ๋‚˜์€ ์œ ์ง€ ๋ณด์ˆ˜์™€ ์ƒ์‚ฐ์„ฑ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” SPA(Single Page Application)๋ฅผ ์œ„ํ•œ React์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.



๐Ÿ”Ž React๋ฅผ ์“ฐ๋Š” ์ด์œ ๋Š”?

ํ”ํžˆ๋“ค ๐ŸŒŽReact๋Š” SPA(Single Page Application)๋ฅผ ์œ„ํ•ด ๋งŽ์ด ์“ด๋‹ค๊ณ ๋“ค ํ•˜๋Š”๋ฐ, SPA๋ž€ ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํŽ˜์ด์ง€๋ฅผ ๋™์ ์œผ๋กœ ์ž‘์„ฑํ•ด์„œ ์‚ฌ์šฉ์ž์™€ ์†Œํ†ตํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ธˆ ๋” ์ž์—ฐ์Šค๋Ÿฌ์šด ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋‚˜ ์›น์‚ฌ์ดํŠธ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ์›น ๋ฐฉ์‹์€ ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ๋ฆฌ์†Œ์Šค๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ๋ฐ›์€ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ Œ๋”๋ง ํ•˜๋ฉด์„œ ๋ณ€๊ฒฝ์ด ํ•„์š” ์—†๋Š” ๋ถ€๋ถ„๋“ค์„ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋‹นํžˆ ๋น„ํšจ์œจ์  ์ด์—ˆ๋Š”๋ฐ SPA๊ฐ€ ๋‚˜์˜ค๋ฉด์„œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํ•„์š”ํ•œ ๋ชจ๋“  ์ •์  ๋ฆฌ์†Œ์Šค๋ฅผ ์ตœ์ดˆ์— ํ•œ๋ฒˆ ๋‹ค์šด๋กœ๋“œ ํ•˜๊ณ , ์ดํ›„์— ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€ ์š”์ฒญ ์‹œ ํŽ˜์ด์ง€ ๊ฐฑ์‹ ์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ์„ ์ „๋‹ฌ๋ฐ›์•„ ํŽ˜์ด์ง€๋ฅผ ๊ฐฑ์‹ ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ด์œ ๋กœ React์˜ ๊ฐ€์žฅ ํฐ ํŠน์ง•์ธ Virtual DOM์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ DOM์€ ํŽ˜์ด์ง€๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์ƒˆ HTML๋ฅผ ๋กœ๋“œํ•˜๋ฉด์„œ ๋ฆฌ๋žœ๋”๋ง ํ•˜๋Š”๋ฐ, Virtual Dom์€ React Component๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๊ฐ’์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ ธ์„œ ์‹ค์ œ ๋ณด์ด๊ณ  ์žˆ๋Š” DOM๊ณผ ๋น„๊ตํ•ด์„œ ๋‹ฌ๋ผ์ง„ ๋ถ€๋ถ„๋งŒ ์ฐพ์•„์„œ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ Virtual Dom์œผ๋กœ ์ธํ•ด ๊ฐœ๋ฐœ์„ Component ๋‹จ์œ„๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ๋ฉ๋‹ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ๋Š” UI๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐœ๋ณ„์ ์ธ ๋ทฐ ๋‹จ์œ„์ธ๋ฐ, UI ๊ฐœ๋ฐœ์„ ์กฐ๋ฆฝํ•˜๋“ฏ์ด ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‚˜๋ˆ„์–ด์ง„ ์ปดํฌ๋„ŒํŠธ ๋“ค๋กœ์ธํ•ด ๋‹ค๋ฅธ ํ•„์š”ํ•œ ๋ถ€๋ถ„๋“ค์— ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ด์ง€๊ณ , ์ด๋Š” ์ƒ์‚ฐ์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ํ–ฅ์ƒ์‹œ์ผœ์ค๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๊ฐ๊ฐ์˜ ์ปดํฌ๋„ŒํŠธ๋“ค์ด ๊ฒฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ View๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.



๐Ÿ”Ž React ์‹œ์ž‘ํ•˜๊ธฐ


React ์ž‘์—… ํ™˜๊ฒฝ ์„ธํŒ…

Node.js๊ฐ€ ๊น”๋ ค์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ „์žฌ๋กœ ๊ฐ€๋ฒผ์šด ์˜ˆ์ œ๋กœ React๋ฅผ ์‹œ์ž‘ํ•ด ๋ณด๋ คํ•ฉ๋‹ˆ๋‹ค.

Node.js / npm version์ด๋ฏธ์ง€ 1. Node.js / npm version


์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด react ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜์ง€๋งŒ,

1
$ npx create-react-app my-app

์ €๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ์‚ฌ์šฉ์„ฑ์— ์ต์ˆ™ํ•ด์ง€๊ธฐ ์œ„ํ•ด React์—์„œ typescript ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค.

1
$ npx create-react-app my-app --template typescript

Begin React์ด๋ฏธ์ง€ 2. Begin React

npx create-react-app my-app --template typescript๋ฅผ ์ž…๋ ฅํ•˜๋ฉด Success ๋ฉ”์„ธ์ง€์™€ ํ•จ๊ป˜ my-app ๋””๋ ‰ํ„ฐ๋ฆฌ์† React Project๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

1
$ npx start

๊ทธ ํ›„ ์ด ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด http://localhost:3000/๊ฐ€ ์—ด๋ฆฌ๋ฉด์„œ ๋บ‘๊ธ€ ๋บ‘๊ธ€ ๋Œ๊ณ  ์žˆ๋Š” React ์•„์ด์ฝ˜์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

React-localhost:3000์ด๋ฏธ์ง€ 3. ๋‚˜์˜ ์ฒซ React ์›น


๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋กœ React TypeScript ์—ฐ์Šต

1. Counter

์šฐ์„  my-app/src/App.tsx๋กœ ๊ฐ€๋ฉด http://localhost:3000/์—์„œ ๋ณด์•˜๋˜ ํ…์ŠคํŠธ์™€ ๋กœ๊ณ ๊ฐ€ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import React from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

App.tsx๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
import * as React from 'react';

import { Counter } from './Counter';

const App = () => {
  return (
    <div className='App'>
      <Counter />
    </div>
  );
};

export default App;

๊ทธ๋ฆฌ๊ณ  src/Counter.tsx ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import * as React from 'react';
import { useState } from 'react';

interface Istate {
    counter: number
}

export function Counter() {
    const [state, setState] = useState<Istate>({  //[์ƒํƒœ ๊ฐ’ ์ €์žฅ ๋ณ€์ˆ˜, ์ƒํƒœ ๊ฐ’ ๊ฐฑ์‹  ํ•จ์ˆ˜] = useState(์ƒํƒœ ์ดˆ๊ธฐ ๊ฐ’);
        counter: 0
    })

    const onIncrement = () => {
        setState({
            counter: state.counter+1
        })
    }

    const onDecrement = () => {
        setState({
            counter: state.counter-1
        })
    }

    return(
        <div>
            <h2>Counter</h2>
            <div>
                {state.counter}
            </div>
            <div>
                <button onClick={onIncrement}>+</button>
                <button onClick={onDecrement}>-</button>
            </div>
        </div>
    )
}

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋™์ ์ธ ๊ฐ’์„ ์ƒํƒœ(state)๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, ์‚ฌ์šฉ์ž ์ธํ„ฐ๋ž™์…˜์„ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ ๊ฐ’์ด ๋™์ ์œผ๋กœ ๋ฐ”๋€” ๊ฒฝ์šฐ์— ์ƒํƒœ ๊ด€๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์›๋ž˜ ์ƒํƒœ ๊ฐ’์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ํด๋ž˜์Šค ๊ธฐ๋ฐ˜์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, ์œ ์ง€ ๋ณด์ˆ˜๊ฐ€ ํž˜๋“ค๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ Hooks๋ผ๋Š” ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜์—ˆ๊ณ  Hooks์˜ useState() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ”๋€Œ๋Š” ๊ฐ’์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


์ด๋ฏธ์ง€ 4. ์นด์šดํ„ฐ ๊ฒฐ๊ณผ



๋๋งบ์Œ

์˜ค๋Š˜์€ React์— ๋Œ€ํ•ด ํฌ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 2์›” ์ดˆ ๋ถ€ํ„ฐ React์— ๋Œ€ํ•ด ์ฐจ๊ทผ์ฐจ๊ทผ ๊ณต๋ถ€๋ฅผ ํ–ˆ๋Š”๋ฐ, ๊ทธ๋™์•ˆ ๊ณต๋ถ€ํ–ˆ๋˜ ๋‚ด์šฉ์„ ์ˆœ์„œ๋Œ€๋กœ ๊ณ„์† ํฌ์ŠคํŠธํ•ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.



[์ฐธ๊ณ ์ž๋ฃŒ]

comments powered by Disqus