Redux์ ํ๋ฆ์ ๋ฐฐ์ฐ๋ฉด์ ํ์ด๋ถ๊ณผ ํจ๊ป ๊ณผ์ ๋ฅผ ์งํํ๋ค.
Action(๊ฐ์ฒด) โ Dispatch(ํจ์) โ Reducer(ํจ์) โ Store(์ ์ฅ์)
๋ฐ์ดํฐ๊ฐ ๋จ๋ฐฉํฅ์ผ๋ก ํ๋ฅธ๋ค.
1. ์ํ๊ฐ ๋ณ๊ฒฝ๋์ด์ผ ํ๋ ์ด๋ฒคํธ ๋ฐ์ ์ ๋ณ๊ฒฝ๋ ์ํ์ ๋ํ ์ ๋ณด๊ฐ ๋ด๊ธด Action ๊ฐ์ฒด๊ฐ ์์ฑ
2. Action ๊ฐ์ฒด => Dispatch ํจ์ ์ธ์๋ก ์ ๋ฌ
3. Dispatch ํจ์ => Action ๊ฐ์ฒด๋ฅผ Reducer ํจ์๋ก ์ ๋ฌ
4. Reducer ํจ์ => Action ๊ฐ์ฒด์ ๊ฐ์ ํ์ธํ๊ณ , ๊ทธ ๊ฐ์ ๋ฐ๋ผ ์ ์ญ ์ํ ์ ์ฅ์ Store์ ์ํ ๋ณ๊ฒฝ
5. ์ํ๊ฐ ๋ณ๊ฒฝ๋๋ฉด, React๋ ํ๋ฉด์ ๋ค์ ๋ ๋๋ง
createStore
๋ฉ์๋๋ฅผ ์ด์ฉํด Reducer ์ฐ๊ฒฐ ํ Store ์์ฑํ ์ ์๋ค.//createStore ๋ถ๋ฌ์ค๊ธฐ
import {createStore} from 'redux';
// createStore๋ฉ์๋ ์ด์ฉํด store ์์ฑ
// ์ธ์๋ reducer ํจ์๋ฅผ ๋ฃ์ด์ค๋ค.
const store = createStore(rootReducer)
// ๊ธฐ๋ณธ ๋ฌธ๋ฒ
const reducer = () => {}
// store ์ธ์๋ reducer
const store = createStore(reducer)
.
.
.
const count = 1
// Reducer๋ฅผ ์์ฑํ ๋์๋ ์ด๊ธฐ ์ํ๋ฅผ ์ธ์๋ก ์๊ตฌํ๋ค.
// state = count = 1
const counterReducer = (state = count, action) => {
// Action ๊ฐ์ฒด์ type ๊ฐ์ ๋ฐ๋ผ ๋ถ๊ธฐํ๋ switch ์กฐ๊ฑด๋ฌธ
switch (action.type) {
//action === 'INCREASE'์ผ ๊ฒฝ์ฐ
case 'INCREASE':
return state + 1
// action === 'DECREASE'์ผ ๊ฒฝ์ฐ
case 'DECREASE':
return state - 1
// action === 'SET_NUMBER'์ผ ๊ฒฝ์ฐ
case 'SET_NUMBER':
return action.payload
// ํด๋น ๋๋ ๊ฒฝ์ฐ๊ฐ ์์ ๋ ๊ธฐ์กด ์ํ๋ฅผ ๊ทธ๋๋ก ๋ฆฌํด
default:
return state;
}
}
// store ์ธ์๋ reducer
const store = createStore(counterReducer)
โ๏ธ์ฌ๋ฌ ๊ฐ์ Reducer๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
combineReducers
๋ฉ์๋๋ฅผ ์ฌ์ฉํด ํ๋์ Reducer๋ก ํฉ์ณ์ค ์ ์๋ค.// combineReducers ๋ถ๋ฌ์ค๊ธฐ import { combineReducers } from 'redux'; // combineReducers ์ฌ์ฉํ๊ธฐ const rootReducer = combineReducers({ //Reducer ํจ์ ์ถ๊ฐํด ์ฃผ๊ธฐ counterReducer, anyReducer, ... })
//payload๊ฐ ํ์ ์๋ ๊ฒฝ์ฐ
{ type: 'INCREASE' }
// payload๊ฐ ํ์ํ ๊ฒฝ์ฐ
{ type: 'SET_NUMBER', payload: 5 }
โ๏ธ ์ก์ ์์ฑ์(Action Creator) : Action์ ์ง์ ์์ฑํ๊ธฐ ๋ณด๋จ Action ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํจ์๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
// payload๊ฐ ํ์ ์๋ ๊ฒฝ์ฐ const increase = () => { return { type: 'INCREASE' } } // payload๊ฐ ํ์ํ ๊ฒฝ์ฐ const setNumber = (num) => { return { type: 'SET_NUMBER', payload: num } }
// Action ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ๋ ๊ฒฝ์ฐ
dispatch( { type: 'INCREASE' } );
dispatch( { type: 'SET_NUMBER', payload: 5 } );
// ์ก์
์์ฑ์(Action Creator)๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
// export๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด ๋ค๋ฅธ ํ์ผ์์ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
export const increase = () => {
return {
type: 'INCREASE'
}
}
export const setNumber = (num) => {
return {
type: 'SET_NUMBER',
payload: num
}
}
dispatch( increase() );
dispatch( setNumber(5) );
useDispatch()
, useSelector()
๋ฉ์๋๋ฅผ ๊ธฐ์ตํ๋ฉด ์ข๋ค.// Redux Hooks ๋ฉ์๋๋ 'redux'๊ฐ ์๋๋ผ 'react-redux'์์ ๋ถ๋ฌ์จ๋ค.
// useDispatch ๋ถ๋ฌ์ค๊ธฐ
import { useDispatch } from 'react-redux';
// ์ธ๋ถ ํ์ผ์์ ์์
ํ increase,decrease ๋ถ๋ฌ์ค๊ธฐ
import { increase,decrease } from './index.js';
const dispatch = useDispatch()
dispatch( increase() )
dispatch( setNumber(5) )
// Redux Hooks ๋ฉ์๋๋ 'redux'๊ฐ ์๋๋ผ 'react-redux'์์ ๋ถ๋ฌ์จ๋ค.
// useSelector ๋ถ๋ฌ์ค๊ธฐ
import { useSelector } from 'react-redux
const counter = useSelector(state => state)
Redux์ ํ๋ฆ์ ์ดํดํ๋ฉด์ ์ด๋ฒ ๊ณผ์ ๋ฅผ ์งํํ๋ค.
์ฝ๊ฐ ์ถ์์ ์ธ ๊ฐ๋
์ด ๋ง์์ ํท๊ฐ๋ฆฌ๋ ๋ถ๋ถ์ด ์๊ธฐ๋ ํ๋ค.
๊ธ๋ก ๋ณด๊ณ ์ดํดํ๊ธฐ ๋ณด๋ค๋ ๊ณผ์ ๋ฅผ ํ์ด๋ณด๋ฉด์
์์ฑํ๊ณ ์ด๋ป๊ฒ ํ๋ฆ์ด ์งํ๋๋์ง ํ์ธ ํ๋
์ดํดํ๋๋ฐ ๋์์ด ๋ ๊ฑฐ ๊ฐ๋ค.
์ด๋ฒ ํ์ด๋ถ์ด ๋๋ฌด ์ ํ์๊ธฐ๋ ํ๊ณ
์ฝ๋์คํ
์ด์ธ ๊ฐ ์๋ํ ๊ณผ์ ์ฃผ์ ๋ฅผ ์ ํ์
ํด ์ฃผ์
์
๊ณผ์ ์ดํ ์งํ๋๋ ์์ต ์๊ฐ์ ํ์ต ๋ฐฉํฅ์ ์ ์ก์ ์ ์์๋ ๊ฑฐ ๊ฐ๋ค.
React, Redux์ ์ํ๋ฅผ ์ด๋ป๊ฒ ๊ด๋ฆฌํ๋์ง
๊ทธ๋ฆผ์ ๊ทธ๋ฆฌ๋ฉด์ ์งํ์ ํด์ผ ๋์ ๋๋ค.