#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

์ฝ”๋“œ ์ˆ˜์ •

  1. ๋ฌธ์ž์—ด์„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธ ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ธฐ ์–ด๋ ค์›€ ADD / MINUS๋ฅผ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉํ•จ

  2. if๋ฌธ์„ switch๋กœ ๋ณ€๊ฒฝ if๋ฌธ์— ๋น„ํ•ด ํ™•์ธํ•˜๊ธฐ ํŽธํ•จ


Last updated

Was this helpful?