Redux

Reference: redux docs, velopert

1. Redux ๋ž€?

  • React ์ƒํƒœ๊ณ„์—์„œ ๊ฐ€์žฅ ์‚ฌ์šฉ๋ฅ ์ด ๋†’์€ ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

  • Redux๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด

    • ์ปดํฌ๋„ŒํŠธ๋“ค์˜ ์ƒํƒœ ๊ด€๋ จ ๋กœ์ง๋“ค์„ ๋‹ค๋ฅธ ํŒŒ์ผ๋“ค๋กœ ๋ถ„๋ฆฌ์‹œ์ผœ์„œ ๋”์šฑ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ

    • ๊ธ€๋กœ๋ฒŒ ์ƒํƒœ ๊ด€๋ฆฌ๋„ ์†์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค

2. Redux vs Context API

Redux๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ Context API์˜ ์ฐจ์ด

2-1. Middleware

  • Redux์—๋Š” middleware๋ผ๋Š” ๊ฐœ๋…์ด ์กด์žฌํ•œ๋‹ค

    • Redux์˜ middleware๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด action object๊ฐ€ reducer์—์„œ ์ฒ˜๋ฆฌ๋˜๊ธฐ ์ „์— ์›ํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค

      • ex)

        • ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ action์ด ๋ฌด์‹œ๋˜๊ฒŒ ๋งŒ๋“ค๊ธฐ

        • action์„ console์— ์ถœ๋ ฅํ•˜๊ธฐ

        • action์ด dispatch ๋˜์—ˆ์„ ๋•Œ, ์ด๊ฒƒ์„ ์ˆ˜์ •ํ•ด์„œ reducer์— ์ „๋‹ฌ๋˜๊ฒŒ ๋งŒ๋“ค๊ธฐ

        • ํŠน์ • action์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ

          • ์ด๊ฒƒ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๋‹ค๋ฅธ action์ด ๋ฐœ์ƒ๋˜๊ฒŒ ํ•˜๊ธฐ

          • ํŠน์ • JavaScript ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ

  • Middleware๋Š” ์ฃผ๋กœ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค

2-2. Useful Functions & Hooks

  • connect ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด redux์˜ ์ƒํƒœ ๋˜๋Š” action ์ƒ์„ฑ ํ•จ์ˆ˜๋ฅผ component์˜ props๋กœ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค

  • useSelector, useDispatch, useStore ๊ณผ ๊ฐ™์€ Hooks๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์†์‰ฝ๊ฒŒ ์ƒํƒœ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ action์„ dispatch ํ•  ์ˆ˜ ์žˆ๋‹ค

2-3. Global State

  • Context API๋ฅผ ์‚ฌ์šฉํ•ด์„œ global state ๋ฅผ ๊ด€๋ฆฌํ•  ๋•Œ์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ธฐ๋Šฅ๋ณ„๋กœ Context๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด,

    • Redux๋Š” ๋ชจ๋“  Global state๋ฅผ ํ•˜๋‚˜์˜ ์ปค๋‹ค๋ž€ object์— ๋„ฃ์–ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ•„์ˆ˜์ด๋‹ค

      • ์ด๊ฒƒ์„ ํ†ตํ•ด ๋งค๋ฒˆ Context ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“œ๋Š” ๋ถˆํŽธํ•จ์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค!

3. Key Concepts

3-1. Action

  • State์— ์–ด๋–ค ๋ณ€ํ™”๊ฐ€ ํ•„์š”ํ•  ๋•Œ, action์„ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค

  • Action์€ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ ํ‘œํ˜„๋œ๋‹ค

    • ex)

      {
        type: "VALUE"
      }
  • Action ๊ฐ์ฒด๋Š” type์„ ํ•„์ˆ˜๋กœ ๊ฐ€์ ธ์•ผ ํ•˜๊ณ , ๊ทธ ์™ธ์˜ ๊ฐ’๋“ค์€ ์›ํ•˜๋Š”๋Œ€๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค

    • ex)

      {
        type: "TODO",
        data: {
          id: 0,
          text: "์ง‘ ๊ฐ€๊ธฐ"
        }
      }
    • ex)

      {
        type: "CHANGE_INPUT",
        text: "์ง‘์— ๊ฐ€์„ธ์š”"
      }

3-2. Action Creator

  • Action ์ƒ์„ฑ ํ•จ์ˆ˜๋Š” ๋ง ๊ทธ๋Œ€๋กœ aciton์„ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜๋‹ค

  • Parameter๋ฅผ ๋ฐ›์•„์™€์„œ action ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค

  • ex)

    export function addTodo(data) {
      return {
        type: "ADD_TODO",
        data
      };
    }
    
    // Arrow function๋„ ๊ฐ€๋Šฅ!
    export const changeInput = text => ({ 
      type: "CHANGE_INPUT",
      text
    });
  • Action Creater ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋‚˜์ค‘์— component์—์„œ action์„ ์‰ฝ๊ฒŒ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋‹ค

    • ๊ทธ๋ž˜์„œ ๋ณดํ†ต export keyword ๋ถ™์—ฌ์„œ ๋‹ค๋ฅธ file์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ๋‚ด์„œ ์‚ฌ์šฉํ•œ๋‹ค

  • Redux์—์„œ Action Creater ์‚ฌ์šฉ์ด ํ•„์ˆ˜๋Š” ์•„๋‹ˆ๋‹ค!

    • Action์„ ๋ฐœ์ƒ์‹œํ‚ฌ ๋•Œ๋งˆ๋‹ค action ๊ฐ์ฒด๋ฅผ ์ž‘์„ฑํ•ด์„œ ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค

3-3. Reducer

  • Reducer๋Š” ๋ณ€ํ™”๋ฅผ ์ผ์œผํ‚ค๋Š” ํ•จ์ˆ˜๋‹ค

  • ๋‘ ๊ฐ€์ง€ parameter๋ฅผ ๋ฐ›์•„์˜จ๋‹ค

    • state

    • action

      • ex)

        function reducer(state, action) {
          // Update status
          return alteredState;
        }
  • Reducer๋Š” ํ˜„์žฌ ์ƒํƒœ์™€ ์ „๋‹ฌ ๋ฐ›์€ action์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ƒํƒœ๋ฅผ ๋งŒ๋“ค์–ด์„œ returnํ•œ๋‹ค

3-4. Store

  • Rexux์—์„œ๋Š” ํ•œ application๋‹น ํ•˜๋‚˜์˜ store๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋œ๋‹ค

  • Store ์•ˆ์—๋Š” ํ˜„์žฌ์˜ app ์ƒํƒœ์™€ reducer๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ๊ณ , ๊ฑฐ๊ธฐ์— ๋ช‡๊ฐ€์ง€ ๋‚ด์žฅ ํ•จ์ˆ˜๋“ค์ด ์žˆ๋‹ค

3-5. Dispatch

  • dispatch๋Š” store์˜ ๋‚ด์žฅ ํ•จ์ˆ˜ ์ค‘ ํ•˜๋‚˜๋‹ค

  • dispatch๋Š” action์„ ๋ฐœ์ƒ ์‹œํ‚ค๋Š” ์—ญํ• ์„ ํ•œ๋‹ค

    • dispatch ํ•จ์ˆ˜์—๋Š” action์„ parameter๋กœ ์ „๋‹ฌํ•œ๋‹ค

      • ex)

        dispatch (action)
  • dispatch๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด store๋Š” reducer๋ฅผ ์‹คํ–‰์‹œ์ผœ์„œ ํ•ด๋‹น action์„ ์ฒ˜๋ฆฌํ•˜๋Š” logic์ด ์žˆ๋‹ค๋ฉด, ๊ทธ action์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ƒํƒœ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค

3-6. Subscribe

  • subscribe๋„ dispatch์ฒ˜๋Ÿผ store์˜ ๋‚ด์žฅ ํ•จ์ˆ˜ ์ค‘ ํ•˜๋‚˜์ด๋‹ค

  • subscribe ํ•จ์ˆ˜์— ํŠน์ • ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๋ฉด, action์ด dispatch ๋˜์—ˆ์„ ๋•Œ ๋งˆ๋‹ค ์ „๋‹ฌํ•œ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค

  • react-redux ๋ผ๋Š” library์—์„œ ์ œ๊ณตํ•˜๋Š” connect ํ•จ์ˆ˜ ๋˜๋Š” useSelector Hook์„ ์‚ฌ์šฉํ•˜์—ฌ redux store์˜ ์ƒํƒœ์— subscribe ํ•œ๋‹ค

4. Three Principles

Redux๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ 3๊ฐ€์ง€ fundamental principles๋ฅผ ์ง€์ผœ์•ผํ•œ๋‹ค

4-1. Single source of truth

  • ํ•˜๋‚˜์˜ application์—๋Š” ๋‹จ ํ•œ๊ฐœ์˜ store๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•œ๋‹ค

    • ์—ฌ๋Ÿฌ๊ฐœ์˜ store๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ์€ ํ•˜์ง€๋งŒ ๊ถŒ์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค!

4-2. State is read-only

  • React์—์„œ state๋ฅผ update ํ•ด์•ผ ํ•  ๋•Œ, setState๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , array๋ฅผ update ํ•ด์•ผ ํ•  ๋•Œ๋Š” array ์ž์ฒด์— pushํ•˜์ง€ ์•Š๊ณ  concat ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ์กด array๋Š” ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด array๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ต์ฒดํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ updateํ•œ๋‹ค

  • Redux์—์„œ๋„ ์ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๊ธฐ์กด์˜ state๋Š” ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด state๋ฅผ ์ƒ์„ฑํ•˜์—ฌ updateํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•ด์ฃผ๋ฉด,

    • ๋‚˜์ค‘์— ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋ฅผ ํ†ตํ•ด์„œ ๋’ค๋กœ ๋Œ๋ฆด ์ˆ˜๋„ ์žˆ๊ณ , ๋‹ค์‹œ ์•ž์œผ๋กœ ๋Œ๋ฆด ์ˆ˜๋„ ์žˆ๋‹ค

  • Redux์—์„œ immutability๋ฅผ ์œ ์ง€ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ data๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์„ ๊ฐ์ง€ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ shallow equality ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ธฐ ๋Œ€๋ฌธ์ด๋‹ค

    • ์ด๊ฒƒ์„ ํ†ตํ•ด ๊ฐ์ฒด์˜ ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€ ํ•  ๋•Œ ๊ฐ์ฒด์˜ ๊นŠ์ˆ™ํ•œ ์•ˆ์ชฝ๊นŒ์ง€ ๋น„๊ต๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฒ‰ํ•ฅ๊ธฐ ์‹์œผ๋กœ ๋น„๊ต๋ฅผ ํ•˜์—ฌ ์ข‹์€ ์„ฑ๋Šฅ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค

4-3. Changes are made with pure functions

  • ์•„๋ž˜์˜ 3๊ฐ€์ง€ ์‚ฌํ•ญ์„ ์ฃผ์˜ํ•˜์—ฌ pure function์„ ๋งŒ๋“ค์•ผ ํ•œ๋‹ค

    • Reducer ํ•จ์ˆ˜๋Š” ์ด์ „ ์ƒํƒœ์™€ action ๊ฐ์ฒด๋ฅผ parameter๋กœ ๋ฐ›๋Š”๋‹ค

    • ์ด์ „์˜ ์ƒํƒœ๋Š” ์ ˆ๋Œ€๋กœ ๊ฑด๋“ค์ด์ง€ ์•Š๊ณ , ๋ณ€ํ™”๋ฅผ ์ผ์œผํ‚จ ์ƒˆ๋กœ์šด ์ƒํƒœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ returnํ•œ๋‹ค

    • ๋˜‘๊ฐ™์€ parameter๋กœ ํ˜ธ์ถœ๋œ reducer ํ•จ์ˆ˜๋Š” ์–ธ์ œ๋‚˜ ๋˜‘๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ’์„ returnํ•ด์•ผ ํ•œ๋‹ค

Last updated