์ค๋์ ๋ฆฌ๋์ค์ ํต์ฌ ๋ฉค๋ฒ์ธ 5ํ์ ๋ฅผ ์์๋ณด์.
- Action
- Reducer
- Store
- Dispatch
- Subscribe
์๋ค์ ์์๋ณผ ๊ฒ์ธ๋ฐ ์ผ๋จ ์ค๋ ๊ฐ์ฅ ์ค์ํ ๊ฒ์ ํ๋ฆ์ ์์์ผํ๋ค.
๋ค๋ฅธ ์์ ๋ค๊ณผ ๋ค๋ฅด๊ฒ ์ด๋ฒ ๊ธ์ ๋์ค๋ ์์ค์ฝ๋๋ ๋ณธ์ธ์ด ์ง์ ํ์ดํ ํ๋ ๊ฒ์ ๊ถ์ฅํ์ง ์๋๋ค.
๊ทธ๋ฅ ํ๋ฆ๋ง ์ดํดํ๋๋ฐ ์จ ํ์ ์์๊ฒ์ ์ถ์ฒํ๋ค.
Redux
์ฐ๋ฆฌ๋ ์ง๋ ์๊ฐ ์ ๋ฆฌ๋์ค๋ฅผ ์์์ผํ๋์ง์ ๋ฆฌ๋์ค๋ ์ด๋ค ํน์ง์ ๊ฐ๊ณ ์๋์ง ์์๋ณด์๋ค.
์ค๋ ์๊ฐ์๋ ๋ฆฌ๋์ค๋ฅผ ๊ตฌ์ฑํด๋ ํต์ฌ 5ํ์ ์ ๋ํด์ ์์๋ณผ ๊ฒ์ด๋ค.
- Action
- Reducer
- Store
- Dispatch
- Subscribe
์๋ค์ด ๋ค์๊ณผ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ๋๊ฒ ๋๋ค.
์ด ๊ทธ๋ฆผ์ ์ ๋ณด๊ณ ํ๋ ํ๋ ๋ฐ๋ผ๊ฐ๋ณด์.
์ฐ์ ์์ํ๊ธฐ ์ ์ ๋ฆฌ๋์ค๋ฅผ ํจํค์ง๋ก ์ค์น๋ฅผ ํด์ผํ๋ค.
$ npm install redux
$ npx i redux
$ yarn add redux
๊ฐ๊ฐ ํจํค์ง ๊ด๋ฆฌ์์ ๋ง๊ฒ ์ค์นํด์ฃผ๊ณ ์์ํ์.
Store
๋ฆฌ๋์ค ํจํค์ง๋ฅผ ์ค์นํ๋ฉด ์ฐ๋ฆฌ๋ ๋ฆฌ๋์ค์์ createStore
๋ผ๋ ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
import { createStore } from "redux";
์ด createStore๋ ํจ์๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ฃผ๋ ์ ์ฅ์์ ์ญํ ์ ์ํํ๋ค.
์ฐ๋ฆฌ ์ฑ์์ ๋ฐ์ดํฐ๋ผ๊ณ ํ๋ค๋ฉด ๋ณดํต State๋ฅผ ๋ฐ์ดํฐ๋ผ๊ณ ํ๋๋ฐ, ์ ํํ State๊ฐ ๋ญ์ง ํ ๋ฒ ์ด์ผ๊ธฐ ํด๋ณด์.
State๋ Modified๋๋ Data ์ด๋ค.
์ด๋ค ๊ฐ์ด ๋ณํ๋ฉด ๊ทธ ๊ฐ์ ๋ฐ๋ผ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ง๋ ๋ถ๋ถ์ด ๋ฌ๋ผ ์ง๊ฒ ํ๋๊ฒ ๋ฆฌ์กํธ์์์ State์์ผ๋ฉด Redux์ State ๋ ๋๊ฐ๋ค.
๋ง์ฝ์ ์ฐ๋ฆฌ๊ฐ ์นด์ดํฐ ์ฑ์ ๋ง๋ ๋ค๊ณ ์น๋ฉด ์ฌ๋ผ๊ฐ๋ ์ซ์๊ฐ state๊ฐ ๋๋ ๊ฑฐ๋ค.
์ด๋ฐ state๋ฅผ store์ ์ง์ด ๋ฃ๋ ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฑธ ๊ฐ๋ฅํ๊ฒ ํ๋ ๊ฒ์ด ๋ฐ๋ก createState
์ด๊ณ .
ํ์ง๋ง ์ฌ๊ธฐ์ store์ createStore
state๋ฅผ ๋ฃ๋ ๊ฒ์ด ์๋๋ผ state๋ฅผ ๋ณ๊ฒฝ์ํค๋ ํจ์๋ฅผ ๋ฃ์ด์ผ ํ๋ค.
๊ทธ ํจ์๊ฐ ๋ฐ๋ก reducer๊ฐ ๋๋ ๊ฒ์ด๋ค.
const reducer = () => {};
const store = createStore(reducer);
๊ทธ๋ฆฌ๊ณ ์ด๋ฐ store๋ฅผ ์ฝ์๋ก ์ถ๋ ฅํ๋ฉด 4๊ฐ์ง์ ํจ์๊ฐ ๋์จ๋ค.
const reducer = () => {};
const store = createStore(reducer);
console.log(store);
>>> dispatch, subscribe, getState, replaceReducer
์ฌ๊ธฐ์ replaceReducer๋ ์ ๊น ์ ์ณ๋๊ณ ์์ 3๊ฐ๋ฅผ ์ง์คํด๋ณด์.
- dispatch : ๋ค์ ๋์ฌ dispatch๋ก ์ ๊น ์ด์ผ๊ธฐ ํ์๋ฉด action์ ๋ฐ์์์ผ state๋ฅผ ๋ณํ๊ฒ ๋ง๋๋ ์ฅ๋ณธ์ธ
- subscribe : state ๊ฐ์ด ๋ณํ๋์ง ๊ฐ์ํ๋ cctv ๊ฒฉ
- getState : state๋ฅผ ์ป์ ์ ์๊ฒ ํ๋ getter ํจ์
๊ทธ๋ผ ์ฐ๋ฆฌ๋ ๋ฆฌ๋์ค์ store๋ฅผ ๋ง๋ค์๋ค.
reducer
reducer๋ ์์ ๋ง ํ๋ ๊ฒ ์ฒ๋ผ ํจ์์ด๋ค.
์ด๋ค ํจ์์ผ๊น?
๋ฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ ํจ์์ด๋ค.
์์์ ์ฐ๋ฆฌ๊ฐ ๋ฐ์ดํฐ์ state๋ฅผ ๋ถ๋ฆฌํด์ ์๊ฐํ๋๋ฐ, data๋ณด๋ค ๋ ๊ตฌ์ฒด์ ์ธ ๊ฒ์ด state์ด๋ผ๊ณ ํ๋ค.
state๋ ๋ณํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ปํ๊ณ ์ด reducer ํจ์๋ ๋ฐ๋ก state๋ฅผ ๋ณ๊ฒฝ์ํค๋ ํจ์์ด๋ค.
์ด ํจ์๊ฐ return ํ๋ ๊ฐ์ด ๋ฐ๋ก state๊ฐ ๋๋ ๊ฒ์ด๋ค.
๋ง์ฝ ๋ด๊ฐ ๋ฆฌ๋์ ํจ์์ ๋ฐํ์ผ๋ก "Hello"๋ผ๋ ์คํธ๋ง์ ์ค๋ค๋ฉด store์ ์๋ state๋ Hello๊ฐ ๋๋ค.
const reducer = () => {
return "hello";
}
const store = createStore(reducer);
console.log(store.getState());
>>> hello
ํน์
const reducer = (state = 0) => {
return state;
}
const store = createStore(reducer);
console.log(store.getState());
>>> 0
์ด๋ ๊ฒ๋ ์ฌ์ฉํ ์ ์๋๋ฐ, ์ฌ๊ธฐ์ ์ฐ๋ฆฌ๋ ์ด๋ป๊ฒ reducer๋ฅผ ์ฌ์ฉํ๋?
- reducer์๊ฒ ์ธ์๋ฅผ ๋๊ธด๋ค.
- reducer๋ ์ธ์๋ฅผ ๋ฐ๊ณ ์ด๋ค ๋ก์ง์ ์คํ์ํฌ์ง ๊ฒฐ์ ํ๋ค.
- ํด๋น ๋ก์ง์ ์ํํ ๊ฒฐ๊ณผ๋ก state๋ฅผ ๋ฐํํ๋ค.
- ๋ฐํ๋ state๋ ์ฐ๋ฆฌ์ ์๋ก์ด state๊ฐ ๋๋ค.
์ฌ๊ธฐ์ ๋งํ๋ ์ธ์๋ ๋ฐ๋ก Action์ด๋ค.
Action
๋ง์ฝ ์ฐ๋ฆฌ๊ฐ ์นด์ดํ ํ๋ ์ฑ์ ๋ง๋ ๋ค๊ณ ํ๋ฉด reducer์์ state + 1์ด๋ state - 1์ ์ํํด์ผ ๋๋๋ฐ, ์๋ค์ ์ด๋ป๊ฒ ๊ตฌ๋ถํ ๊น?
๋ฐ๋ก Action์ผ๋ก ๊ตฌ๋ถํ๋ค.
const reducer = (state, action) => {
return state;
}
const store = createStore(reducer);
reducer์ ๋ ๋ฒ์งธ ์ธ์๋ก Action์ด ๋ค์ด๊ฐ๋๋ฐ, ์ด Action์ ์ฐ๋ฆฌ๊ฐ reducer์ ์ํตํ๋ ๋งค๊ฐ์ฒด์ด๋ค.
reducer์๊ฒ ์ด๋ค ์ผ์ ํ ์ง Action์ผ๋ก ๋ํํ๋๊ฑฐ๊ณ reducer๋ action์ ๋ฐ๋ผ์ returnํด์ฃผ๋๊ฑฐ๋ค.
๊ทธ๋ผ ์ด action์ ์ด๋ป๊ฒ ์ ๋ฌํด์ฃผ๋๊ณ ?
๋ฐ๋ก Dispatch์ด๋ค.
Dispatch
์์์ ์ฐ๋ฆฌ๊ฐ store๋ฅผ ์ฝ์๋ก ์ถ๋ ฅํ๋ฉด dispatch๊ฐ ๋์ค๋๊ฒ์ ํ์ธ ํ์ ๊ฒ์ด๋ค.
๊ทธ dispatch๋ฅผ ์ด์ฉํด์ ์ฐ๋ฆฌ๋ action์ ์ ๋ฌํ๋ค. store.dispatch()
์ ๋งค๊ฐ๋ณ์์ ์ด๋ค Action์ธ์ง ๊ฐ์ฒด ํํ๋ก ๋ณด๋ด์ค๋ค.
const reducer = (state, action) => {
return state;
}
const store = createStore(reducer);
store.dispatch({type: "ADD"})
๊ทธ๋ฆฌ๊ณ ์ฌ๊ธฐ์ ์กฐ๊ฑด์ ์กฐ๊ธ ์ถ๊ฐํด๋ณด์๋ฉด
const reducer = (state = 0, action) => {
if(action.type === "ADD") { return state + 1;}
else return state;
}
const store = createStore(reducer);
store.dispatch({type: "ADD"});
store.dispatch({type: "ADD"});
store.dispatch({type: "ADD"});
store.dispatch({type: "ADD"});
console.log(store.getState());
>>> 4
๊ฐ ๋๋ค.
๋ด๊ฐ ADD๋ผ๋ Action์ ๊ฐ๋ dispatch๋ฅผ ์ด 4๋ฒ ํธ์ถํ๊ธฐ ๋๋ฌธ์ 4๊ฐ ์ถ๋ ฅ๋๋ ๊ฒ์ด๋ค.
Subscribe
Subscribe๋ ๋ณํ๋ฅผ ๊ฐ์งํ๋ค.
Dispatch๋ฅผ ํตํด์ Reducer์ state๋ฅผ ๋ณ๊ฒฝ์ํฌ ๋ ๋ง๋ค Subscribe๋ ์์ฐ์์ฐํ๋๋ฐ, ๊ทธ ์์ฐ์์ฐ์ ํตํด์ ์ฐ๋ฆฌ๋ ๋ณํ๋ฅผ ๊ฐ์งํ๋ค.
Subscribe ์ญ์ ๋งค๊ฐ๋ณ์๋ก ํจ์๊ฐ ๋ค์ด๊ฐ๊ฒ ๋๋๋ฐ, ๊ทธ ํจ์๋ ๋ณํ๊ฐ ์์ ๋ ์ด๋ค ํ๋์ ํ ์ง์ ๋ํ ํจ์์ด๋ค.
๋ง์ฝ ๋ด๊ฐ
const reducer = (state = 0, action) => {
if(action.type === "ADD") state + 1;
return state;
}
const store = createStore(reducer);
store.dispatch({type: "ADD"})
const onChange() => {
console.log("changed");
}
store.subscribe(onChnage);
์ด๋ ๊ฒ ์ฃผ๋ฉด
const reducer = (state = 0, action) => {
if(action.type === "ADD") state + 1;
return state;
}
const store = createStore(reducer);
const onChange() => {
console.log("changed");
}
store.subscribe(onChnage);
store.dispatch({type: "ADD"})
store.dispatch({type: "ADD"})
store.dispatch({type: "ADD"})
store.dispatch({type: "ADD"})
store.dispatch({type: "ADD"})
>>> changed
>>> changed
>>> changed
>>> changed
>>> changed
๋ณํ๊ฐ ๊ฐ์ง๋ ๋ ๋ง๋ค subscribe๊ฐ ํธ์ถ๋๋ค.
์ ์ด๋ ๊ฒ ์ค๋์ Redux์ ํต์ฌ ์ปจ์ ๊ณผ 5ํ์ ์ ๋ํด์ ์์๋ณด์๋ค.
์ฌ์ค ๋ค๋ฅธ ๊ธ์ ์ง์ ํ์ดํ ํ๋ ๊ฒ์ ์ถ์ฒํ์ง๋ง ์ด๋ฒ ๊ธ์ ๊ทธ๋ฅ ๋ฐ๋ผ์ค๋ ๊ฒ์ ์ถ์ฒํ๋ค.
๋ง์ฝ ๋น์ ์ด ๋ฆฌ๋์ค์ ๋ํด์ ์ฒ์ ์๊ฒ ๋์๋ค๋ฉด ํ์๋ฅผ ์น๋๋ผ ํต์ฌ์ ๋์น ๊ฒ์ด ๋๋ ค์์์ด๋ค.
๋ด ๋ธ๋ก๊ทธ๋ง ๋ณด๊ณ ๊ณต๋ถ๋ฅผ ํ๋ ์ฌ๋์ ์๊ฒ ์ง๋ง ๋ ธํ์ฌ์ ๋ง ํ์๋ฉด ๋ค๋ฅธ ๋ธ๋ก๊ทธ๋ ์ถฉ๋ถํ ๋ด์ผํ๋ค.
๊ณต์ ๋ฌธ์๋ผ๋ฉด ๋ ์ข๊ณ .
๋๊ธ