๐Ÿ“† 22.10.13 - Redux Toolkit ?

๋ฒ„๋“คยท2022๋…„ 10์›” 13์ผ
0

โœจToday I Learn (TIL)

๋ชฉ๋ก ๋ณด๊ธฐ
7/62
post-thumbnail

์ด์ „์— redux... ์ •~~๋ง ๋ถˆํŽธํ–ˆ๋‹ค ๊ฐœ์ธ์ ์œผ๋กœ...
๊ทธ๋Ÿฐ๋ฐ ๋‹คํ–‰ํžˆ๋„ redux์˜ ์ข€ ๋” ์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•œ? ๋ฒ„์ „์ธ redux toolkit์„ ํ•ญํ•ด ์ฃผํŠน๊ธฐ ๋งˆ์ง€๋ง‰ ์ฃผ์ฐจ์—์„œ์•ผ ์ ‘ํ•  ๊ธฐํšŒ๊ฐ€ ์ƒ๊ฒจ ์ •๋ฆฌํ•ด ๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

Redux toolkit?

์ผ๋‹จ Redux Toolkit์€ Redux์˜ ๊ฐœ๋Ÿ‰ ๋ฒ„์ „์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.
๊ธฐ์กด์˜ ๋ฆฌ๋•์Šค๋Š” ์›๋ž˜ ์‚ฌ์šฉ๋˜์—ˆ๋˜ ducks ํŒจํ„ด์˜ ์š”์†Œ๋“ค์ด ์ „์ฒด์ ์ธ ์ฝ”๋“œ์˜ ์–‘์„ ๋Š˜๋ฆฐ๋‹ค๋Š” ๊ฐœ๋ฐœ์ž๋“ค์˜ ๋ถˆ๋งŒ์ด ํ•˜๋‚˜ ๋‘˜ ๋“ค๋ฆฌ๊ธฐ ์‹œ์ž‘ํ–ˆ๊ณ , ์ด๋ฅผ ์ธ์ง€ํ•œ redux ๊ฐœ๋ฐœ์ง„๋“ค์€ ์ฝ”๋“œ๋ฅผ ๋” ์ ๊ฒŒ ๊ทธ๋ฆฌ๊ณ  ๋ฆฌ๋•์Šค๋ฅผ ๋” ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๋ณ€ํ™”๋ฅผ ๋„์ž…์„ ํ•˜์˜€๋‹ค.

๊ทธ๋ ‡๋‹ค๊ณ  ๊ธฐ์กด์˜ redux ์™€ ์•„์˜ˆ ๋‹ฌ๋ผ์ง€์ง„ ์•Š์•˜๋‹ค. ๋ฐ”๋€ ๋ถ€๋ถ„์„ ํ•œ๋ฒˆ ์ด์•ผ๊ธฐ ํ•ด๋ณด์ž!

redux toolkit install

npm install react-redux @reduxjs/toolkit

์œ„์˜ ์ฝ”๋“œ๋กœ reduxjs/toolkit ๋ฒ„์ „์œผ๋กœ ์„ค์น˜ํ•˜๋ฉด ๋œ๋‹ค.

๋‹ฌ๋ผ์ง„ ์ ์„ ์ฝ”๋“œ๋กœ ํ™•์ธํ•ด๋ณด์ž! (ex. count ํ”„๋กœ๊ทธ๋žจ)

// redux (toolkit x) 

// Action Value
const ADD_NUMBER = "ADD_NUMBER";
const MINUS_NUMBER = "MINUS_NUMBER";

// Action Creator
export const addNumber = (payload) => {
  return {
    type: ADD_NUMBER,
    payload,
  };
};

export const minusNumber = (payload) => {
  return {
    type: MINUS_NUMBER,
    payload,
  };
};

// Initial State
const initialState = {
  number: 0,
};

// Reducer
const counter = (state = initialState, action) => {
  switch (action.type) {
    case ADD_NUMBER:
      return {
        number: state.number + action.payload,
      };
    // [ํ€ด์ฆˆ ๋‹ต]
    case MINUS_NUMBER:
      return {
        number: state.number - action.payload,
      };
    default:
      return state;
  }
};

// export default reducer
export default counter;

vs

// redux toolkit

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

const initialState = {
  number: 0,
};

// ํ•˜๋‹จ์˜ reducers ๋ถ€๋ถ„์—์„œ ๋งŒ๋“ค์–ด์ง„ ํ•จ์ˆ˜ ์ž์ฒด๊ฐ€ ๋ฆฌ๋“€์„œ์˜ logic์ด ๋œ๋‹ค. = action
const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    addNumber: (state, action) => {
      state.number = state.number + action.payload;
    },

    minusNumber: (state, action) => {
      state.number = state.number - action.payload;
    },
  },
});

// ์•ก์…˜ํฌ๋ฆฌ์—์ดํ„ฐ๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด export ํ•˜๊ณ 
export const { addNumber, minusNumber } = counterSlice.actions;
// reducer ๋Š” configStore์— ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด export default ํ•ฉ๋‹ˆ๋‹ค.
export default counterSlice.reducer;

๊ธฐ์กด์— redux์—์„œ ๋‹ค๋ฃฌ ์š”์†Œ์ธ Action Value, Action Creator, Reducer ๋ถ€๋ถ„์„ ๋”ฐ๋กœ๋”ฐ๋กœ ๊ตฌ๋ถ„์ง€์–ด ์ •ํ•ด์ง„ ์ฝ”๋“œ ๊ทœ์น™ ๋“ฑ์„ ๋„ฃ์–ด์คฌ๋‹ค๋ฉด, toolkit ์€ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์กŒ๋‹ค ๋ผ๋Š” ๋ถ€๋ถ„์ด ๊ฐ€์žฅ ํฐ ๋ณ€๊ฒฝ์ ์ด์ž ์žฅ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

์žฅํ™ฉํ•˜๊ฒŒ ๋‚˜์—ด๋˜๋Š” ๊ฒƒ๋ณด๋‹จ, ๊ฐ™์€ ๊ธฐ๋Šฅ์ด์ง€๋งŒ ๊ฐ€๋…์„ฑ์„ ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•ด ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์“ฐ์ด๋Š” ๋ถ€๋ถ„์ด ๋” ์ข‹์ง€ ์•Š๋‚˜..
( ๊ฐœ๋ฐœ์ž๋ผ๋Š” ์ง์—…์„ ๋ชฉํ‘œ๋กœ ํ•˜๋‹ค ๋ณด๋‹ˆ ๋ญ”๊ฐ€ ์ถ”ํ›„์— ๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ๊ฐ€ ์ง€์ €๋ถ„ํ•˜๊ฑฐ๋‚˜ ๋„ˆ๋ฌด ๊ธธ์–ด ํ™•์ธํ•˜๊ธฐ ์–ด๋ ค์šฐ๋ฉด ์ข€ ๋งŽ์ด ๋ถˆํŽธํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™๋‹ค. )

๊ทธ๋ฆฌ๊ณ  ์ผ๋ฐ˜ ๋ฆฌ๋•์Šค์—์„œ ํ–ˆ๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ export๋ฅผ ํ†ตํ•ด์„œ ๋‚ด๋ณด๋‚ด๊ณ  ์‚ฌ์šฉํ•ด์•ผ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ importํ•˜๋ฉด ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค! ํ•˜๋‹จ์ด ๊ทธ ์˜ˆ์ด๋‹ค

// ์•ก์…˜ํฌ๋ฆฌ์—์ดํ„ฐ๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด export ํ•˜๊ณ 
export const { addNumber, minusNumber } = counterSlice.actions;
// reducer ๋Š” configStore์— ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด export default ํ•ฉ๋‹ˆ๋‹ค.
export default counterSlice.reducer;

๊ทธ๋Ÿฐ๋ฐ ์ด์ „์— vuex ๋ฅผ ์ผ๋˜ ๋‚˜์—๊ฒŒ๋Š” ์•„์ง๋„ ์ด ์–‘๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ์ „์†ก์˜ ๋ง›์„ ๋ชป์žŠ์–ด ์‚ด์ง ๋ถˆํŽธํ•œ ์ ์ด ์žˆ๋‹ค. ์ข€ ๋” ์—ฌ๋Ÿฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ•œ๋ฒˆ ์จ๋ณด๋ฉด ๋‚ด๊ฒŒ ๋งž๋Š” ์ „์—ญ ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์ง€ ์•Š๋‚˜ ์‹ถ๋‹ค!

์ด ๋ถ€๋ถ„์€ ํ•ญํ•ด99 ์˜ reference๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์Šค์Šค๋กœ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

profile
ํƒœ์–ด๋‚œ ๊น€์— ๋งŽ์€ ๊ฒฝํ—˜์„ ํ•˜๋ ค๊ณ  ์•„๋“ฑ๋ฐ”๋“ฑ ์• ์“ฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž

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