๊ฐœ๋ฐœ ๐Ÿพ/ReactJS

๋ชจ๋‘๊ฐ€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” React ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ •๋ฆฌ

JOTOKKI 2021. 2. 6. 23:02
728x90

๋ฆฌ์•กํŠธ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ ๋ณดํ†ต ๋ฌด์กฐ๊ฑด ์„ค์น˜ํ•˜๊ณ  ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 

์ด๋ฅผ ๋ฏธ๋ฆฌ ์ •๋ฆฌํ•ด ๋‘๋ฉด ํŽธ๋ฆฌํ•  ๊ฒƒ ๊ฐ™์•„ ์ž‘์„ฑํ•ด๋ด…๋‹ˆ๋‹ค. 

 

๋ฆฌ์•กํŠธ ์‚ฌ์šฉ์ž๋“ค์€ ๋Œ€๋ถ€๋ถ„ ์•„๋Š” ๋‚ด์šฉ์ผ ๊บผ๋ผ ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค๋งŒ, 

ํ˜น์‹œ ๋ฆฌ์•กํŠธ๋ฅผ ์ด์ œ ๋ง‰ ์‹œ์ž‘ํ•˜์‹œ๋Š” ๋ถ„๋“ค์€ ๋„์›€์ด ๋˜์‹ค๊บผ๋ผ ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค. 

 

์ œ๊ฐ€ ์ž‘์„ฑํ•˜๊ณ ์ž ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

Router, Polyfill, Axios, Redux ์ž…๋‹ˆ๋‹ค. 

์•ž์œผ๋กœ ๋ฆฌ์•กํŠธ์— ๊ด€๋ จ๋œ ์ •๋ณด๊ธ€์„ ์ž‘์„ฑํ•˜๋ฉด์„œ ์ข€ ๋” ์„ธ์„ธํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ฒ ์ง€๋งŒ, 

์šฐ์„ ์€ ์–ด๋–ค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ์ง€๋Š” ์•Œ์•„์•ผ ํ•˜๋‹ˆ, ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ ์„ค๋ช…์„ ๊ฐ™์ด ์ ์–ด ๋‘์—ˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜Š


1. Router

๋ผ์šฐํ„ฐ๋Š” ์ฃผ์†Œ์— ๋”ฐ๋ผ ๋ถ€๋ถ„ ๋ Œ๋”๋ง์„ ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. 

SPA์—์„œ ํŽ˜์ด์ง€ ์ด๋™์„ ๊ตฌํ˜„ํ•˜๋ฉด ๊ฑฐ์˜ ํ•„์ˆ˜๋ผ๊ณ  ์ƒ๊ฐํ•˜์…”๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค. 

์˜›๋‚  ๋ฐฉ์‹ ์ฒ˜๋Ÿผ ์ „์ฒด ํŽ˜์ด์ง€๋ฅผ ์ „ํ™˜ํ•˜๊ณ  ๋ฆฌ์†Œ์Šค๋ฅผ ๋‹ค์‹œ ๋‹ค์šด ๋ฐ›์ง€ ์•Š๊ณ , ๋ฏธ๋ฆฌ ๋ฐ›์•„๋‘์—ˆ๋˜ ๋ฆฌ์†Œ์Šค๋“ค์„ ์ฃผ์†Œ์— ๋”ฐ๋ผ ๋ถ€๋ถ„ ์ ์œผ๋กœ ๋ณ€๊ฒฝํ•ด ์ฃผ๋Š” ๊ฑฐ๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

npm install react-router-dom --save
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";

function App() {
  return (
    <Router>
      <div>
        <Link to="/about">About</Link>
        <Link to="/service">Service</Link>
        <Link to="/qna">Qna</Link>
        <hr />
        <Switch>
          <Route exact path="/" component={Main} />
          <Route exact path="/about" component={About} />
          <Route exact path="/service" component={Service} />
          <Route exact path="/qna" component={Qna} />
        </Switch>
      </div>
    </Router>
  );
}

์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ์‚ฌ์‹ค ํ”„๋กœ์ ํŠธ์— ๋”ฐ๋ผ ๋‹ค๋ฅด์ง€๋งŒ, ์ด๊ฒŒ ์•„๋งˆ ๋‹ค๋“ค ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ์ผ๊ป๋‹ˆ๋‹ค.

Link ํƒœ๊ทธ์— url ์ฃผ์†Œ๋ฅผ ๋„ฃ๊ณ , ํ•ด๋‹น ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด ํ•˜๋‹จ์— Route์˜ path์™€ ๋น„๊ตํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฟŒ๋ ค์ค๋‹ˆ๋‹ค. 

์•„์ฃผ ๊ฐ„๋‹จํ•˜์ง€์š”? ๋ผ์šฐํŠธ์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์•„์ฃผ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. 

 

2. Polyfill

๋ฆฌ์•กํŠธ๋Š” ES6๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— IE์—์„œ๋Š” ์ž‘๋™์ด ์•ˆ๋˜๋Š” ๋ฌธ๋ฒ•์ด ๋งŽ์Šต๋‹ˆ๋‹ค. 

์„œ๋น„์Šค ํ™˜๊ฒฝ์ด ie์— ๊ตญํ•œ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด ์ƒ๊ด€ ์—†์ง€๋งŒ, ํ•œ๊ตญ์—์„œ๋Š” ie ์„œ๋น„์Šค๋ฅผ ์•ˆํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ์ฐพ๊ธฐ ํž˜๋“ค์ง€์š”. 

์ •๋ง ํ”„๋กœ์ ํŠธ ํ•  ๋•Œ ๋งˆ๋‹ค ํ™”๊ฐ€ ๋‚˜์„œ IE์‚ฌ์šฉ์„ ๋ฒ•์ ์œผ๋กœ ๊ธˆ์ง€ํ–ˆ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค๋Š” ๋ง๋„ ์•ˆ๋˜๋Š” ์ƒ์ƒ์„ ํ•˜๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜…

์•„๋ฌดํŠผ ํด๋ฆฌํ•„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ชจ๋“  ๊ฒƒ์„ ๋ฐ›์•„์ฃผ๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. 

ํด๋ฆฌํ•„ ์ข…๋ฅ˜๋„ ๋งŽ๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋งˆ๋‹ค ์ถ”๊ฐ€๋กœ ์ง€์›ํ•ด ์ฃผ๋Š” ๊ฒƒ๋„ ์žˆ์œผ๋‹ˆ, ์–ด๋– ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ์—๋Š” ๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ๋„ ์‚ดํŽด ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. 

๊ทธ๋ž˜๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์น˜ํ•ด ์ค๋‹ˆ๋‹ค. ์„ค์น˜ ์•ˆํ•˜๋ฉด ์•„์˜ˆ IE ์—์„œ๋Š” ์•ˆ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค. 

์ด๋ฏธ ์˜ค๋ž˜์ „์— ๋ฆฌ์•กํŠธ๊ฐ€ ๊ณต์‹์ ์œผ๋กœ IE๋ฅผ ๋ฒ„๋ ธ๊ฑฐ๋“ ์š”. 

npm install react-app-polyfill

ํ˜น์‹œ ํ”„๋กœ์ ํŠธ ์ค‘๊ฐ„์— ์„ค์น˜ ํ•˜๊ฒŒ๋˜๋ฉด ์ œ๋Œ€๋กœ ๋™์ž‘์ด ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

๊ทธ๋Ÿด๋•Œ๋Š” react-app-polyfill ์„ค์น˜ ํ›„ ์บ์‹œ์‚ญ์ œ ํ•˜๊ณ  ์žฌ์‹คํ–‰ ํ•ด์ฃผ์„ธ์š”.

์บ์‹œ ์‚ญ์ œ๋Š” node_modules/.cache ๋””๋ ‰ํ„ฐ๋ฆฌ ์‚ญ์ œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  Polyfill ์‚ฌ์šฉ์„ ํ•˜๋ ค๋ฉด ๋ฃจํŠธ์— ์žˆ๋Š” index.js ํŒŒ์ผ์— import ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

// IE9์˜ ๊ฒฝ์šฐ
import 'react-app-polyfill/ie9';
import 'react-app-polyfill/stable';

// IE11์˜ ๊ฒฝ์šฐ
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';

๊ทธ๋ฆฌ๊ณ  package.json ํŒŒ์ผ์•ˆ์—๋„ ์ถ”๊ฐ€ํ•ด ์ฃผ์„ธ์š”. 

"browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version",
      "ie 11"   // <- ์ถ”๊ฐ€
    ]
  }  

๊ทธ๋ฆฌ๊ณ  npm start ๋ฅผ ํ•˜๋ฉด ๊ทธ๋ž˜๋„ ํ™”๋ฉด์ด ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 

์ด๋Š” ์‚ฌ์‹ค ์ €๋„ ์ •ํ™•ํ•˜๊ฒŒ ์•„๋Š”๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. 

build ์‹œ์—๋งŒ ์ ์šฉ์ด ๋œ๋‹ค๋Š” ๋ง์ด ์žˆ์–ด์„œ, build๋ฅผ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 

( ํ˜น์‹œ ์•„์‹œ๋Š” ๋ถ„ ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์„ธ์š”! )

npm run build 
npx serve -s build 

build ์ดํ›„ ์—๋Š” ํ™”๋ฉด์ด ์ž˜ ๋œน๋‹ˆ๋‹ค. 

 

3. Axios

axios๋Š” ๋น„๋™๊ธฐ ํ†ต์‹ ์— ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ http ํ†ต์‹ ์„ ํ•ด์•ผํ•œ๋‹ค๋ฉด ๊ฑฐ์˜ ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 

APIํ†ต์‹ ์„ ์œ„ํ•œ request์™€ response ์‚ฌ์ด์—์„œ interceptor๋ฅผ ํ†ตํ•œ ์ฒ˜๋ฆฌ๋„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ณ , Pomise๋ฅผ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜๊ธฐ ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ์ธ์ ์œผ๋กœ ๊ธฐ์กด ajax๋ณด๋‹ค๋Š” ํ›จ์”ฌ ์‚ฌ์šฉํ•˜๊ฒŒ ํŽธํ•ฉ๋‹ˆ๋‹ค. 

npm install axios --save

 

axios๋„ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ํŒจํ„ด์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 

๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ์ด๋Ÿฌํ•˜๋ฉฐ, ์‚ฌ์šฉ์‹œ finally๋Š” ๊ผญ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. 

axios.get('your url')
	.then(response => {
    	console.log("response => ", response);
    })
    .catch(error => {
    	console.log("Error => ", Error);
    })
    .finally(() => {
    	console.log("The End!");
    })

 

 

4. Redux (ํ•„์ˆ˜์•„๋‹˜)

๋ฆฌ์•กํŠธ์—์„œ ์ƒํƒœ์˜ ๋ณ€ํ™”๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ๋Š” ์ฐธ ๋ฒˆ๊ฑฐ๋กญ์Šต๋‹ˆ๋‹ค. 

์ดˆ๋ฐ˜ ์Šคํ„ฐ๋”” ํ• ๋•Œ์—๋Š” ๋ณ„๋กœ ํž˜๋“ค์ง€ ์•Š์€๋ฐ? ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค๊ฐ€, ๋ณต์žกํ•œ ํ”„๋กœ์ ํŠธ์—์„œ ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์—†์ด props์„ ๋ฏธ์นœ๋“ฏ์ด ์ฃผ๊ณ  ๋ฐ›์•„ ๊ณค์š•์„ ์น˜๋ฅธ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

์‚ฌ์‹ค ์ €๋„ ๋ฆฌ์•กํŠธ๋ฅผ ์•„์ง ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค๋งŒ, ๋ฆฌ๋•์Šค๋Š” ์ฐธ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. 

๋น„๋™๊ธฐ ํ†ต์‹ ์ด ์‹œ์ž‘๋˜๋ฉด์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ๋„ˆ๋ฌด ๋งŽ๊ณ  ์ƒํ™ฉ์— ๋”ฐ๋ผ ์‚ฌ์šฉ๋˜๋Š” ํŒจํ„ด๋“ค๋„ ๋Š˜์–ด๋‚˜์„œ ์ •๋ง ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ๊ฒ€์ƒ‰ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ํž˜๋“ค๋”๋ž๋‹ˆ๋‹ค.. [๊ทธ๋ƒฅ ์ œ๊ฐ€ ์‹ค๋ ฅ์ด ๋ถ€์กฑํ•ด์„œ๐Ÿ˜ฅ]

 

์ •๋ฆฌํ•˜์ž๋ฉด, ์ดˆ๋ณด์ž์—๊ฒŒ๋Š” ์ง„์ž…์žฅ๋ฒฝ์ด ๋†’์€ ํŽธ์ž…๋‹ˆ๋‹ค. 

๊ตณ์ด ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ์†Œ๊ทœ๋ชจ ํ”„๋กœ์ ํŠธ์—๋Š” ๊ทธ๋ƒฅ props์œผ๋กœ๋งŒ ๊ด€๋ฆฌ๋ฅผ ํ•˜๊ฑฐ๋‚˜ react์ž์ฒด์—์„œ ์ œ๊ณต๋˜๋Š” useContext๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๊ธธ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ถ„๋ช… ๋ฆฌ๋•์Šค์˜ ๊ฐ•์ ์ด ์žˆ์œผ๋ฏ€๋กœ, ์–ด์จŒ๋“  ์‚ฌ์šฉํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ๊ฒ ์ง€์š”?

 

npm install redux react-redux redux-promise redux-thunk --save
npm install redux-devtools-extension

 

์ €๋Š” ์ฃผ๋กœ ์ด๋ ‡๊ฒŒ ํ•œ๊บผ๋ฒˆ์— ์„ค์น˜ํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. 

- redux๋Š” ๋ฆฌ๋•์Šค ์ž์ฒด์ž…๋‹ˆ๋‹ค. 

- react-redux๋Š” ๋ฆฌ์•กํŠธ์—์„œ ๋ฆฌ๋•์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ค‘๊ฐ„ ์—ญํ™œ์„ ํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ผ๊ณ  ์ƒ๊ฐ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

  ์ฆ‰, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ ์…ˆ์ด๋„ค์š”. 

- redux-promise ๋Š” promise๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค. 

- redux-thunk๋Š” function์ฆ‰ ๊ฐ์ฒด ๋Œ€์‹  ํ•จ์ˆ˜ ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค. 

redux-promise์™€ redux-thunk๋Š” ๋ฏธ๋“ค์›จ์–ด๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 

- redux-devtools-extension ์€ ๊ตฌ๊ธ€ ํ™•์žฅ์•ฑ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋ฆฌ๋•์Šค ์‚ฌ์šฉ์‹œ state์™€ action์„ ์‹ค์‹œ๊ฐ„ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์šฉํ•œ ์•ฑ์ž…๋‹ˆ๋‹ค. ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค๋ฉด ์ƒ๋žตํ•ด๋„ ๋˜์ง€๋งŒ, ๋ฆฌ๋•์Šค ์‚ฌ์šฉํ• ๋•Œ๋Š” ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. 

 

๋ฃจํŠธ์ธ index.js ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ ํ•ด ์ค๋‹ˆ๋‹ค. 

import { Provider } from 'react-redux';
import { applyMiddleware, createStore } from 'redux';
import promiseMiddleware from 'redux-promise';
import ReduxThunk from 'redux-thunk';
import Reducer from './_reducers'

const createStoreWithMiddleware = applyMiddleware(promiseMiddleware, ReduxThunk)(createStore);
ReactDOM.render(
  <Provider
    store={createStoreWithMiddleware(Reducer,
      window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()  // redux devtool ๋•Œ๋ฌธ์— ๋„ฃ์Œ
      )}
  >
    <App />
  </Provider>,
  document.getElementById('root')
);

์ดํ›„ ์Šคํ† ์–ด, ๋ฆฌ๋“€์„œ, ์•ก์…˜์„ ์ƒ์„ฑํ•œ ๋’ค ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

์ด๋Š” ๋‚ด์šฉ์ด ์กฐ๊ธˆ ๊ธธ์–ด์งˆ ๊ฒƒ ๊ฐ™์•„ ์•„์ฃผ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋งŒ ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค.

// ์•ก์…˜ ํƒ€์ž…
const ADD = 'add';

// ์•ก์…˜ ์ƒ์„ฑ ํ•จ์ˆ˜
export const add = () => ({ type: ADD })

// ์ดˆ๊ธฐ ์ƒํƒœ
const initialState = {
	number: 0
}

// ๋ฆฌ๋“€์„œ
function add(state=initialState, action) {
	switch(action.type){
    	case ADD:
        	return { number: state.number + 1 }
        default: 
        	throw new Error(`Unable action type ${action.type}`)        	
    }
}

export default MyRedux;

์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

๋ฆฌ๋•์Šค๋Š” ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ๋งŽ์€ ํ•™์Šต์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ๋‹ค์–‘ํ•˜๊ฒŒ ๋‚˜์˜ค๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ๊ด€์‹ฌ์žˆ์œผ์‹  ๋ถ„๋“ค์€ redux-toolkit, redux-saga ๋„ ๊ฐ™์ด ์ฐพ์•„๋ณด์„ธ์š”! 

 

 

 

์ด ์™ธ์—๋„ uuid, cookie, chart, datepicker ๋“ฑ๋“ฑ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์ด ์•„์ฃผ ๋งŽ์Šต๋‹ˆ๋‹ค. 

๊ทธ ์ค‘ ๋ฆฌ์•กํŠธ๋กœ ์‹œ์ž‘ํ•œ๋‹ค๋ฉด, ์†Œ๊ทœ๋ชจ ํ”„๋กœ์ ํŠธ๋„ ํ•„ํžˆ ์„ค์น˜ํ•˜๋ฉด ์ข‹์„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์†Œ๊ฐœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ( redux๋นผ๊ณ  )

ํ”„๋ก ํŠธ ์˜์—ญ์€ ์ •๋ง ๋น ๋ฅด๊ฒŒ ๋ฐœ์ „ ํ•˜๊ณ  ์žˆ์–ด, ํ•™์Šตํ•˜๋ฉฐ ๋”ฐ๋ผ๊ฐ€๊ธฐ ์ •๋ง ํž˜๋“ญ๋‹ˆ๋‹ค. 

์ƒˆ๋กœ์šด ์–ธ์–ด๋‚˜ ํŒจํ„ด์ด ๋‚˜์˜ฌ์ˆ˜๋ก ์ •๋ง ๊ธฐ๋ณธ์„ ํŠผํŠผํ•˜๊ฒŒ ๊ณต๋ถ€ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฑธ ๋งŽ์ด ๋Š๋‚€๋‹ต๋‹ˆ๋‹ค. 

๊ทธ๋Ÿผ ์˜ค๋Š˜๋„ ์ฆ๊ฑฐ์šด ์ฝ”๋”ฉ ํ•˜์„ธ์š”~๐Ÿฅฐ

 

 

 

๋ฐ˜์‘ํ˜•