๐Ÿ‘พRedux Toolkit

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

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

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

01. Redux๊ฐ€ ๋“ค์–ด๊ฐ„ ์šฉ์–ด

  • Redux : ์ƒํƒœ ๊ด€๋ฆฌ์ž๋กœ Redux๋Š” JS ํ”„๋กœ์ ํŠธ๋ผ๋ฉด ์–ด๋””์„œ๋“  ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ Redux ์ž์ฒด๋Š” React ์™€ ๋ฌด๊ด€ํ•˜๋‹ค.

  • React Redux : Redux๋ฅผ React์— ์‚ฌ์šฉํ•˜๊ธฐ์—๋Š” ์–ด๋ ค์›€์ด ์žˆ๋‹ค.

  • Redux Toolkit : ์œ„์—์„œ ๋งํ•œ ์–ด๋ ค์›€์„ ํ•ด๊ฒฐํ•ด์ฃผ๊ธฐ ์œ„ํ•ด Redux์—์„œ ๋‚ด๋†“์€ ํŒจํ‚ค์ง€์ด๋‹ค.

๐Ÿ“ Redux Toolkit์˜ ์žฅ์ 

  • ์ดˆ๊ธฐ ์„ค์ •์ด ๊ฐ„ํŽธํ•˜๋‹ค.
  • Redux Toolkit ๋‚ด๋ถ€์— ๋‹ค์–‘ํ•œ ํŒจํ‚ค์ง€๋“ค์ด ์„ค์น˜๋˜์–ด ์žˆ์–ด Redux devtool ๊ฐ™์€ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
  • ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ์ค„์—ˆ๋‹ค.
  • ๋ถˆ๋ณ€์„ฑ์„ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

02. Redux์™€ Redux Toolkit

์™ผ์ชฝ์— ์žˆ๋Š” ์›์ด Redux์˜ store์ด๋‹ค. Redux์—์„œ๋Š” store ํ•˜๋‚˜์— ๋ชจ๋“  ์ •๋ณด๋ฅผ ๋‹ด์•˜๋‹ค. ํ•˜์ง€๋งŒ ํ”„๋กœ์ ํŠธ๊ฐ€ ์ปค์ง€๋ฉด ํ•˜๋‚˜์— ๋‹ด๊ธฐ๋ณด๋‹ค ๊ธฐ๋Šฅ๋ณ„๋กœ ๋‚˜๋ˆ„์–ด ์ •๋ณด๋ฅผ ๋‹ด์•„์ฃผ๋Š” ๊ฒƒ์ด ์œ ์ง€๋ณด์ˆ˜์— ํšจ์œจ์ ์ด๋‹ค.

์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์›์€ ๊ฐ๊ฐ์˜ ๊ธฐ๋Šฅ์„ ๋‚˜๋ˆ„์–ด ๋‹ด์€ store ์ด๋‹ค. ๊ฐ๊ฐ์˜ ๊ธฐ๋Šฅ์„ ํ•˜๊ณ  ์žˆ๋Š” slice๋Š” ์ž‘์€ store๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. Redux Toolkit์ด ์ž‘์€ store์„ ํ•ฉ์ณ์„œ, Redux๊ฐ€ ์š”๊ตฌํ•˜๋Š” ํฐ store๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.


03. Redux Toolkit ์„ค์น˜ ์ค‘ ์˜ค๋ฅ˜

๐Ÿ”ง Redux Toolkit ์„ค์น˜

 npx create-react-app ์„ค์น˜ ํด๋”๋ช… --template basic-react

์ด๋ ‡๊ฒŒ ์„ค์น˜๋ฅผ ํ•˜๊ณ  npm start๋ฅผ ํ–ˆ๋Š”๋ฐ ์˜ค๋ฅ˜ 3๊ฐœ๊ฐ€ ๋ฐœ๊ฒฌ๋˜์—ˆ๋‹ค.

๐Ÿšจ module not found: error: can't resolve 'react-redux'
๐Ÿšจ module not found error can't resolve '@reduxjs/toolkit
๐Ÿšจ module not found error can't resolve '@reduxjs/toolkit

์ด ์ค‘์—์„œ 2๊ฐœ๋Š” ๊ฐ™์€ ์˜ค๋ฅ˜์˜€๋‹ค. ์•„๋งˆ ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ๊ฐ๊ฐ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜ํƒ€๋‚œ ๊ฒƒ ๊ฐ™๋‹ค. ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ๋ฃจํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ํ„ฐ๋ฏธ๋„์—์„œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๊ณ  ๋‹ค์‹œ ์„œ๋ฒ„๋ฅผ ์‹œ์ž‘ํ•ด์•ผ ํ–ˆ๋‹ค.

๐Ÿ“Œ ์˜ค๋ฅ˜ ํ•ด๊ฒฐ

 npm install redux react-redux
 npm install @reduxjs/toolkit

04. Redux Toolkit ์ฝ”๋“œ

1. store ์ƒ์„ฑ

configureStore ํ•จ์ˆ˜๋ฅผ @reduxjs/toolki์—์„œ ๋ถˆ๋Ÿฌ์™€ store๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. reducer ๋‚ด๋ถ€์—๋Š” slice์˜ reducer์ด ํฌํ•จ๋˜์–ด์•ผ ํ•œ๋‹ค.

const store =  configureStore({
    reducer:{
    }
});
export default store;

2. Provider

์ƒ์„ฑํ•œ store์„ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋“ค์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์†์„ฑ์ด store์ธ <Provider> ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ <Counter> ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ์‹ผ๋‹ค. <Provider> ๋ถ€๋ถ„์€ index.js์— ์ถ”๊ฐ€ํ•ด์ค˜๋„ ๋˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” App.js์— ๋„ฃ์—ˆ๋‹ค.

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

        </Provider>
    );
}

3. slice ์ƒ์„ฑ

slice๋Š” ์ž‘์€ store๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, slice๋“ค์ด ๋ชจ์—ฌ์„œ ํ•˜๋‚˜์˜ ํฐ store๊ฐ€ ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. Redux์—์„œ๋Š” if, switch๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์•ก์…˜์˜ ๋™์ž‘์„ ๋‚˜ํƒ€๋ƒˆ์ง€๋งŒ Redux Toolkit์—์„œ๋Š” object ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

const counterSlice= createSlice({
    name: 'counter',
    initialState: {value:0}, 	// ์ดˆ๊ธฐ๊ฐ’ ์ง€์ •
    reducers: {					// ์•ก์…˜ํƒ€์ž…
        up: (state, action)=>{
            state.value = state.value + action.payload;
        }
    }
});
export default counterSlice;
// dispatchํ•  ๋•Œ ์•ก์…˜์„ ์ „๋‹ฌํ•˜์—ฌ ์ƒํƒœ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ณ€ํ™”์‹œํ‚ฌ์ง€๋ฅผ ๊ฒฐ์ •
export const {up} = counterSlice.actions;

์ด๋ ‡๊ฒŒ createSlice์—๋Š” ์„ธ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.
1. name, 2. initialState, 3.reducers ์ด๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก configureStore ์— ๋“ฑ๋กํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ๋“ฑ๋ก์ด๋ž€ ์ด ๊ธ€์˜ 4. store์— slice ์ถ”๊ฐ€์˜ ๊ณผ์ •์„ ๋งํ•œ๋‹ค.


4. store์— slice ์ถ”๊ฐ€

์ƒ์„ฑํ•œ slice๋ฅผ store์˜ reducer์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

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

์—ฌ๊ธฐ์„œ counter ๋ถ€๋ถ„์€ ๋งˆ์Œ๋Œ€๋กœ ์ž‘๋ช…ํ•ด๋„ ๋˜์ง€๋งŒ, counterSlice์˜ name์ด๋ž‘ ๋˜‘๊ฐ™์ด ๋งž์ถฐ์ฃผ์—ˆ๋‹ค.


5. counter ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ

import React from "react";
import {Provider,useSelector,useDispatch} from 'react-redux';
import store from './store';
import {up} from './counterSlice';

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

useSelector๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ state์ธ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋ฐ›์•„์ค€๋‹ค. ์ด๋•Œ state๋Š” counterSlice ์•ˆ์— ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.


6. App.js์— couter ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€

import React from "react";
import {Provider,useSelector,useDispatch} from 'react-redux';
import store from './store';
import {up} from './counterSlice';

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

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