๐Ÿ‘พRedux Toolkit์„ ์‚ฌ์šฉํ•œ ์นด์šดํ„ฐ ๊ธฐ๋Šฅ ๊ตฌํ˜„

๊ถŒ๊ทœ๋ฆฌยท2023๋…„ 6์›” 7์ผ
0

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ปFrontEnd

๋ชฉ๋ก ๋ณด๊ธฐ
26/29
post-thumbnail

01. ํŒŒ์ผ ๊ตฌ์„ฑ

  • App.js : Counter ์ปดํฌ๋„ŒํŠธ์™€ App( )์œผ๋กœ ๊ตฌ์„ฑ๋œ ํŒŒ์ผ
  • counterSlice.js : createSlice๋กœ slice๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํŒŒ์ผ
  • store.js : configureStore์ด ์žˆ๋Š” ํŒŒ์ผ
  • index.js : ๊ธฐ๋ณธ ํŒŒ์ผ

๐Ÿ“Œ ๊ฒฐ๊ณผ๋ฌผ

ํ”Œ๋Ÿฌ์Šค ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๊ฐ€์šด๋ฐ์— ์žˆ๋Š” ์ˆซ์ž๊ฐ€ 2์”ฉ ์ฆ๊ฐ€ํ•˜๊ณ  ๋งˆ์ด๋„ˆ์Šค ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๊ฐ€์šด๋ฐ์— ์žˆ๋Š” ์ˆซ์ž๊ฐ€ 2์”ฉ ๊ฐ์†Œํ•œ๋‹ค.


02. ์ฝ”๋“œ ๋ฐ ๋ฌธ๋ฒ•

1. createSlice๋ฅผ ์ด์šฉํ•˜์—ฌ slice ์ƒ์„ฑ

import {createSlice} from "@reduxjs/toolkit";

const counterSlice= createSlice({
    name: 'counter',
    initialState: {value:0}, // ์ดˆ๊ธฐ๊ฐ’ ์ง€์ •
    reducers: {
        up: (state, action)=>{
            state.value = state.value + action.payload;
        },
        down: (state, action)=>{
            state.value = state.value + action.payload;
        }
    } 
});
export const {up} = counterSlice.actions;
export const {down} = counterSlice.actions;
export default counterSlice;

reducers ์•ˆ์— up, down ์•ก์…˜ ํƒ€์ž…์ด ์กด์žฌํ•œ๋‹ค. ์ด๋“ค์˜ ๊ธฐ๋Šฅ์€ ๊ฐ๊ฐ 2์”ฉ ์ฆ๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ฐ์†Œํ•˜๋ฉด์„œ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— state์˜ value๋ฅผ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด์„œ, ๋ฐ‘๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

state.value = state.value + action.payload;

2. configureStore ๋ฅผ ์ด์šฉํ•˜์—ฌ store ์ƒ์„ฑ

import {configureStore} from '@reduxjs/toolkit';
import counterSlice from "./counterSlice";

const store =  configureStore({
    reducer:{
        couter:counterSlice.reducer
    }
});

export default store;

counterSlice ์•ˆ์— ์žˆ๋Š” up, down์„ ๋ชจ์€ ๊ฒƒ์ด counterSlice.reducer์ด๋‹ค. counterSlice.reducer๋ฅผ "counter์— ๋Œ€ํ•œ reducer์•ผ ~" ํ•˜๊ณ  configureStore์˜ reducer๋กœ ์ „๋‹ฌํ•œ๋‹ค.


3. store์„ Provider์—๊ฒŒ ์ „๋‹ฌ

export default function App(){
    return (
        <Provider store={store}>
        </Provider>
    );
}

๐Ÿ“ข index.js์— ์ถ”๊ฐ€ํ•ด๋„๋จ !!

  • import { Provider } from "react-redux"; ๊ตฌ๋ฌธ ์ถ”๊ฐ€
  • import configureStore from "./store.js" ๊ตฌ๋ฌธ ์ถ”๊ฐ€
    (๐Ÿšจ ๋ฐ‘์— ์‚ฌ์ง„์—๋Š” ์—†์ง€๋งŒ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•œ๋‹ค.)
  • render ์•ˆ์— Provider๋กœ App์„ ๊ฐ์‹ธ์ค€๋‹ค

Provider๋ฅผ App์— ๊ฐ์‹ธ์ฃผ๋ฉด Store์—์„œ ์ž‘์„ฑํ•œ State ์ƒํƒœ๋“ค์„ ์•ฑ์— ์•ˆ์— ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” Component๋“ค์ด ๋ฝ‘์•„์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.


4. Counter ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ

function Counter(){
    const dispatch = useDispatch();
    const count = useSelector(state=>{
        return state.counter.value;
    });
    return <div>
        <button onClick= {()=>{
            dispatch(up(2));
        }}>+</button>
        {count}
        <button onClick= {()=>{
            dispatch(down(-2));
        }}>-</button>
    </div>
}

return state.couter.value;์—์„œ counter์€ counterSlice์˜ name์ด๋‹ค. ์ด๋•Œ const count์˜ ๊ฐ’์€ couterSlice์˜ initialState์˜ ์ดˆ๊ธฐ๊ฐ’์ด ๋œ๋‹ค.

๐Ÿ”Ž ์›๋ž˜๋Š” ๋ฐ‘๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์ง€๋งŒ ์†์ด ๋งŽ์ด ๊ฐ„๋‹ค. Redux Toolkit์—์„œ๋Š” up, down๊ณผ ๊ฐ™์€ ๋ฆฌ๋“€์Šค ํ•จ์ˆ˜๋“ค์„ ์ฐธ์กฐํ•˜์—ฌ ์ž๋™์œผ๋กœ ์•ก์…˜์„ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค.

 <button onClick= {()=>{dispatch({type:'counter/up', stpe:2});}}>
   + 
 </ button>

๐Ÿค” How to do?

 <button onClick= {()=>{dispatch(counterSlice.actions.up(2);}}>
   + 
 </ button>

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  up์— ๊ฐ€์„œ console.log(action)์„ ํ•˜๋ฉด ์ฝ˜์†”์— payload : 2 ๋ผ๊ณ  ๋œจ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ด๋ ‡๊ฒŒ ์ž๋™์œผ๋กœ ์•ก์…˜์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒƒ์„ ์„ ํƒ ํ–ˆ๋‹ค๋ฉด, ๋ฐ‘๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜๊ณ 

 <button onClick= {()=>{dispatch(up(2);}}>
   + 
 </ button>

์ถ”๊ฐ€๋กœ counterSlice์—์„œ state.value = state.value + action.payload;๋กœ ์ˆ˜์ •ํ•œ๋‹ค.


5. App( )์— counter ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

export default function App(){
    return (
        <Provider store={store}>
            <div>
                <Counter></Counter>
            </div>
        </Provider>
    );
}
profile
๊ธฐ๋ก์žฅ ๐Ÿ“

0๊ฐœ์˜ ๋Œ“๊ธ€