#1 PURE REDUX: COUNTER
#1.0 Vanilla Counter
Vanilla JS
1์ฉ ๋ํ๊ณ ๋นผ๋ ๊ธฐ๋ฅ ๊ตฌํ
#1.1 Store and Reducer
store = state ์ ์ฅํ๋ ๊ณณ
state = ์ดํ๋ฆฌ์ผ์ด์ ์์ ๋ณํ๋ data
createStore() = reducer ์ญํ ์ ํ ํจ์๋ฅผ ์ธ์๋ก ์๊ตฌํจ.
reducer = data๋ฅผ modifyํ๋ ํจ์. reducer๊ฐ returnํ๋ ๊ฒ์ application์ ์๋ data๊ฐ ๋๋ค. (=> state๊ฐ ๋ณ๊ฒฝ๋๋ค)
import { createStore } from "redux";
const reducer = (state) => { // ์ฌ๊ธฐ์ state๋ ์ด๊น๊ฐ
return abcdfefef
}
const store = createStore(reducer) // reducer์ store ์ญํ ๋ถ์ฌ
//reducer์ return ๊ฐ์ด state๊ฐ ๋จ#1.2 Actions
Action - reducer ํจ์์ ๋๋ฒ์งธ ํ๋ผ๋ฏธํฐ
dispatch()๋ฅผ ํตํด {type: value}๋ฅผ ์ ์กํ ์ ์๋ค.
Action์ ๋ฐ๋์ object, type์ด ํฌํจ๋์ด์ผ ํจ
#1.3 Subscriptions
Subscribe - store ๋ด๋ถ์ ๋ณํ ๊ฐ์ง
store.subscribe(func) > store ๋ด๋ถ์ ๋ณํ๋ฅผ ๊ฐ์งํ๋ฉด func ์คํ
#1.4 Recap Refactor
์ฝ๋ ์์
๋ฌธ์์ด์ ๋ณ์๋ก ์ ์ธ ๋ฌธ์์ด์ ์ฌ์ฉํ๋ฉด ์ค๋ฅ๋ฅผ ๋ฐ๊ฒฌํ๊ธฐ ์ด๋ ค์ ADD / MINUS๋ฅผ ๋ณ์๋ก ์ ์ธํด์ ์ฌ์ฉํจ
if๋ฌธ์ switch๋ก ๋ณ๊ฒฝ if๋ฌธ์ ๋นํด ํ์ธํ๊ธฐ ํธํจ
Last updated
Was this helpful?