๐Ÿ‘ฉ‍๐Ÿ’ป/React

[react] redux๋กœ ์ƒํƒœ ๊ด€๋ฆฌํ•˜๊ธฐ #์‹œ์ž‘ํ•˜๋ฉฐ

ํ•œ๋‚˜ 2021. 3. 8. 00:24

์‹œ์ž‘ํ•˜๋ฉฐ

App์ด ์ ์  ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๋ฉด์„œ ์ƒํƒœ ๊ด€๋ฆฌํ•  ํ•„์š”๋ฅผ ๊ณ„์† ๋Š๋ผ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ์ด๋ฒˆ์—์•ผ ๋„์ž…ํ–ˆ๋‹ค. ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•˜๊ณ , ๊ฐ„๋‹จํ•˜๊ฒŒ +/- ๋ฒ„ํŠผ์„ ํ†ตํ•ด ์นด์šดํŠธ๋˜๋Š” ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด๋ณด๋ฉด์„œ Redux์— ์ ‘๊ทผํ•ด๋ดค๋‹ค.

ํ•„์š” ๋ชจ๋“ˆ ๋‹ค์šด๋กœ๋“œ

yarn add react-redux redux redux-actions

yarn์„ ํ†ตํ•ด ์œ„ dependencies๋ฅผ ๋ฐ›์•˜๋‹ค.

ํด๋” ๊ตฌ์„ฑ

์ตœ์ข… ํด๋” ๊ตฌ์„ฑ์€ ์‚ฌ์ง„์ฒ˜๋Ÿผ ๋˜์–ด ์žˆ๋‹ค. src/store ๋‚ด config.js, index.js ํŒŒ์ผ์„ ๊ฐ€์ง€๋ฉฐ, modules ํด๋”๋ฅผ ๋”ฐ๋กœ ๋‘์—ˆ๋‹ค.

store ํด๋”์˜ ๊ตฌ์„ฑ

store

1. config.js ํŒŒ์ผ ์ƒ์„ฑ

ํ•ด๋‹น ํŒŒ์ผ์€ ์—ฌ๋Ÿฌ store๋ฅผ ๋ชจ์•„ ์™ธ๋ถ€ ์ปดํฌ๋„ŒํŠธ์™€ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. store๋Š” state๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ผ์ข…์˜ ๋ณด๊ด€์ฒ˜.

// src/config.js
import { createStore } from 'redux'; // ์—ฌ๋Ÿฌ store๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ ํ™˜๊ฒฝ์„ ๋งŒ๋“ ๋‹ค. 

์šฐ์„  config.js ํŒŒ์ผ์€ ์œ„์ฒ˜๋Ÿผ ๋งŒ๋“ค์–ด๋‘๊ณ  ์‹œ์ž‘ํ•œ๋‹ค.

2. store ๋””๋ ‰ํ† ๋ฆฌ ์•„๋ž˜์— modules ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑ

modules 
โ”” index.js 
โ”” test.js
  • modules : state๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ชจ๋“  store๊ฐ€ ๋‹ด๊ธฐ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋กœ, ์•ž์œผ๋กœ ์ถ”๊ฐ€๋  ๋ชจ๋“  store๋Š” ์ด ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค.
  • test.js : test๋ฅผ ์œ„ํ•œ store ํŒŒ์ผ

2-1. test.js ํŒŒ์ผ ์ž‘์„ฑ

// src/moduels/test.js

import { createAction, handleActions } from 'redux-actions';

const initialState = {
    num: 0
};

export default handleActions({
// ...
}, initialState);

redux-actions๋กœ๋ถ€ํ„ฐ ํ˜ธ์ถœํ•˜๋Š” ๋‘ ํ•จ์ˆ˜๋Š” store ๋‚ด actions๋ฅผ ์ œ์–ดํ•œ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ๋Š” num์ด๋ผ๋Š” ์ด๋ฆ„์˜ state๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๋‹ค.

2-2. index.js ํŒŒ์ผ ์ž‘์„ฑ

// src/modules/index.js
import { combineReducers } from 'redux';

import test from './test';

export default combineReducers({
    test
})

modules ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด store ํŒŒ์ผ์ด ์ฐจ๊ณก ์ฐจ๊ณก ์Œ“์ด๋ฉด, ์ด๋ฅผ ํ•œ๊ณณ์—์„œ ๋ชจ์•„ ๋‚ด๋ณด๋‚ด๋Š” ๊ฒƒ์ด ํ•„์š”ํ•ด์ง„๋‹ค. ์™ธ๋ถ€์—์„œ store๊ฐ€ ํ•„์š”ํ•ด์งˆ ๋•Œ๋Š” src/store/moduels/index.js๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๋ชจ๋“  store์— ์ ‘๊ทผํ•˜๋„๋ก ํ•œ๋‹ค.

3. config.js ํŒŒ์ผ ์ˆ˜์ •

// src/config.js

import { createStore } from 'redux';
import modules from './modules';  // store๊ฐ€ ๋‹ด๊ธด ์ •๋ณด๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 

const configure = () => { // devTools๋ผ๋Š” ๋ณ€์ˆ˜์— Redux Store์˜ ํ™˜๊ฒฝ ์„ค์ •์„ ๋‹ด๋Š”๋‹ค. 
    const devTools = window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__();
    const store = createStore(modules, devTools);

    return store;
}

export default configure;

1๋ฒˆ์—์„œ createStore ํ•จ์ˆ˜๋งŒ ํ˜ธ์ถœํ•˜๊ณ  ๋งŒ config.js๋กœ ๋Œ์•„๊ฐ€ ์œ„์ฒ˜๋Ÿผ ์ž‘์„ฑํ•ด์ค€๋‹ค.

์—ฌ๊ธฐ์„œ createStore์— ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ์„ ํƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, redux์—์„œ ์ œ๊ณตํ•˜๋Š” devTool์„ ์“ฐ๊ณ ์ž ํ•  ๊ฒฝ์šฐ์— ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์šฐ์„  Firefox๋‚˜ Chrome์—์„œ addon ๋˜๋Š” extension์„ ๋ฐ›์•„์ค€๋‹ค.

Firefox addon ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ

์ด๋ฅผ ๋ฐ›์•„์ค€ ํ›„, ์ฝ”๋“œ ์ƒ์—์„œ ํ•ด๋‹น ์„ค์ •์„ ๋งˆ์น˜๋ฉด

Redux Dev Tool

์ด๋Ÿฐ DevTool์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์œ„์— counter์™€ isLogged๊ฐ€ ์žˆ๋Š” ๋ชจ์Šต์€, ํ…Œ์ŠคํŠธ ์‚ผ์•„ counterReducer์™€ isLoggedReducer๋ฅผ ๋งŒ๋“ค๊ณ  ์ดˆ๊นƒ๊ฐ’์„ ๊ฐ๊ฐ 0, false๋กœ ์ง€์ •ํ•ด์ฃผ์–ด์„œ ์ด๋ฅผ ๋ฐ˜์˜ํ•ด ๋‚˜ํƒ€๋‚œ ๋ชจ์Šต์ด๋‹ค.

3-1. index.js ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

// src/store/index.js
import configure from './config';

export default configure();

์ด์ œ config ํ•จ์ˆ˜๋ฅผ ์ข€ ๋” ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•˜๋„๋ก index.js ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด ์œ„์ฒ˜๋Ÿผ ์ž‘์„ฑํ•ด์ค€๋‹ค.

4. redux๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

4-1. src/index.js ์ˆ˜์ •

// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
import { BrowserRouter } from 'react-router-dom';
import './config/lang/i18n'

import { Provider } from 'react-redux'; // react-redux ๋ชจ๋“ˆ๋กœ๋ถ€ํ„ฐ Provider๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 
import Store from './store/index.js'; // store์— ์•„๊นŒ ์ž‘์„ฑํ•œ index.js๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. 

ReactDOM.render(
  <Provider store={Store}>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </Provider>,
  document.getElementById('root')
);

๋ฒˆ์—ญ์„ ์œ„ํ•ด i18n ๋ชจ๋“ˆ๊ณผ router ์‚ฌ์šฉ์„ ์œ„ํ•ด BrowserRouter๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ๋Š” ๋ชจ์Šต์ด๋‹ค. ์›๋ž˜ <BrowserRouter> ํƒœ๊ทธ๋ฅผ ๊ฐ์‹ธ๋Š” ํƒœ๊ทธ๋กœ, <React.StrictMode>๊ฐ€ ์žˆ์—ˆ๋‹ค. ํ•ด๋‹น ํƒœ๊ทธ๋ฅผ <Provider store={Store}>๋กœ ์ˆ˜์ •ํ•ด์ค€๋‹ค.

4-2. src/App.js ์ˆ˜์ •

// src/App.js
import React, { Component } from 'react';
import './App.css';
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as testAction from './store/modules/test';

class App extends Component {

  render() {
    return(
      <div className='App'>
        <h1> Redux Test </h1>

        <div>
          <button> + </button>
          {/* num */}
          <button> - </button>
        </div>
      </div>
    )
  }
}

export default App;

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๋ฉด ์œ„์ฒ˜๋Ÿผ UI๋ฅผ ์ž‘์„ฑํ•ด์ค„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ํ•จ์ˆ˜ํ˜•์œผ๋กœ ์ž‘์„ฑ๋œ App.js๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” ์ฝ”๋“œ ํ•˜๋‹จ์— App์˜ defaultProps๋ฅผ ์ •์˜ํ•ด์ค€๋‹ค.

import React from 'react';
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as testAction from './store/modules/test';

function App() {
    return (
          <div className='App'>
        <h1> Redux Test </h1>

        <div>
          <button> + </button>
          {/* num */}
          <button> - </button>
        </div>
      </div>
    )
}

App.defaultProps = {
  num: 0 // component์˜ Props๋“ค์˜ ๊ธฐ๋ณธ ๊ฐ’์„ ์ง€์ •ํ•œ๋‹ค. 
}
export default connect( // react-redux ๋ชจ๋“ˆ์˜ ๊ธฐ๋Šฅ, store์™€ store์˜ state ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค. 
  (state) => ({  // ์‚ฌ์šฉํ•  state๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค. 
    num: state.test.num // state.storeName.stateName ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค. 
  }),
  (dispatch) => ({ // ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ์‚ฌ์šฉํ•  Action ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค. 
    testAction: bindActionCreators(testAction, dispatch), // ์ธ์ž๊ฐ’์œผ๋กœ store๋กœ๋ถ€ํ„ฐ ํ˜ธ์ถœ๋œ ์ •๋ณด๋ฅผ ๋‹ด๋Š”๋‹ค. 
  })
)(App); // ํ˜„์žฌ ์ปดํฌ๋„ŒํŠธ์˜ ์ด๋ฆ„
  • num: state.test.num ์ฝ”๋“œ์˜ test๋Š” ๋ฐ˜๋“œ์‹œ modules/index.js์— combineReducers ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋‹ด์€ ๊ฐ์ฒด์— ์ž‘์„ฑํ•œ ๊ฒƒ๊ณผ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.

test๋ผ๊ณ  ์ž‘์„ฑํ•œ ์ด๋ฆ„๊ณผ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.

  • state ์—ญ์‹œ test.js์—์„œ ์ž‘์„ฑํ•œ initialState์— num ๋ณ€์ˆ˜๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

4-3. ์ปดํฌ๋„ŒํŠธ์—์„œ state ๊ฐ’ ๋ฟŒ๋ ค์ฃผ๊ธฐ

// src/App.js

// ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ๊ฒฝ์šฐ this.props ๋ฅผ ํ†ตํ•ด num์— ์ ‘๊ทผํ•œ๋‹ค. 
<button> + </button>
ใ€€{ this.props.num }ใ€€
<button> - </button>

// ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ๊ฒฝ์šฐ
function App(props) {
    return (
        <button> + </button>
       { props.num }ใ€€
      <button> - </button>
    )
}

initialState ๊ฐ’์ด ์ œ๋Œ€๋กœ ์ถœ๋ ฅ๋˜๊ณ  ์žˆ๋‹ค.

4-4. ๋ฒ„ํŠผ์„ ์กฐ์ž‘ํ–ˆ์„ ๋•Œ state ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ

์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” Action์„ ์ธ์ž๋กœ ๋ฐ›๋Š” Reducer๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ state ๊ฐ’ ๋ณ€๊ฒฝ์„ ์‹œ๋„ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด Action์„ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
๋‹ค์‹œ, modules ๋””๋ ‰ํ† ๋ฆฌ์˜ test.js๋กœ ๋Œ์•„๊ฐ€ ์•„๋ž˜์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.

// src/modules/test.js
import { createAction, handleActions } from 'redux-actions'; 
// ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ reducer ํ•จ์ˆ˜๋ฅผ ์“ฐ๊ธฐ ์œ„ํ•ด Acton์„ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋ฏ€๋กœ, Action์„ ์ƒ์„ฑํ•˜๊ณ , ์ด๋ฅผ ๋‹ค๋ฃจ๋Š” ํ•จ์ˆ˜๋ฅผ redux-actions ๋ชจ๋“ˆ๋กœ๋ถ€ํ„ฐ ํ˜ธ์ถœํ•œ๋‹ค. 

const CHANGENUMBER = 'test/change_number'; 
// ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ณ€์ˆ˜์— Action์— ๋Œ€ํ•œ ๊ฒฝ๋กœ๋ฅผ ๋‹ด๋Š”๋‹ค. ์ด ๊ฒฝ๋กœ๊ฐ’์€ ์ค‘๋ณต๋˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค. 

export const change_number = createAction(CHANGENUMBER); 
// ๊ฒฝ๋กœ๋ฅผ ๋‹ด์€ ๋ณ€์ˆ˜๋ฅผ createAction ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋„ฃ๋Š”๋‹ค. 
// ์ด์ œ ์™ธ๋ถ€์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก exportํ•œ๋‹ค. 


const initialState = {
    num: 0
}

export default handleActions({ 
      // handleActions์˜ ์ธ์ž๋กœ ๋“ค์–ด๊ฐ€๋Š” ๊ฐ์ฒด์— ํ‚ค๊ฐ€ ๋œ CHANGENUMBER ์ƒ์ˆ˜. 
    [CHANGENUMBER] : (state, data) => { 
        let num = state.num;
        // ์ฒซ ๋ฒˆ์งธ ์ธ์ž์— ๊ธฐ์กด state ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ๋‹ด๋Š”๋‹ค.
        // ๋‘ ๋ฒˆ์งธ ์ธ์ž์—๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ reducer ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๋Š”๋‹ค. 

      if (data.payload.bool) {
            num += 1;
        } else {
            num -= 1;
        }

        return {
            ...state,   // ์ด ์ „๊ฐœ ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด Action ์ž‘๋™ ์‹œ ๋ชจ๋“  state๊ฐ€ ์ดˆ๊ธฐํ™”๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•œ๋‹ค.
            num: num
        }


    }
}, initialState);

์ด์ œ ์ปดํฌ๋„ŒํŠธ์—์„œ reducer ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” Action์ด ๋งŒ๋“ค์–ด์กŒ๋‹ค. ์ปดํฌ๋„ŒํŠธ๋กœ ๋‹ค์‹œ ๋Œ์•„๊ฐ€์„œ,

// src/App.js
import React from "react";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as testAction from './store/modules/test';

function App(props) {
      const _changeNumber = (bool) => {
      const { testAction } = props; // test store๋ฅผ props๋กœ ํ˜ธ์ถœ
      testAction.change_number({'bool' : bool }) // test store์˜ change_number ๊ฒฝ๋กœ๋กœ ์ „๋‹ฌ
    }

    // ์œ„ ํ•จ์ˆ˜ ์‹คํ–‰ ์‹œ state  ๋ณ€ํ™”๋ฅผ ์ฃผ๋Š” reducer๊ฐ€ ์ž‘๋™ํ•˜๊ณ , ์ด๋•Œ ์ฐธ์กฐ๋˜๋Š” Action์œผ๋กœ test store์— ์ •์˜๋œ change_number์˜ ๊ฒฝ๋กœ๊ฐ€ ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋ฉฐ num state๊ฐ€ ๋ณ€๊ฒฝ๋œ๋‹ค. 

    return (
        <h1>Redux Test</h1>
      <div>
        <button onClick={() => {
          console.log('clicked');
          _changeNumber(true)
        } }>+</button>
        {props.num}
        <button onClick={() => _changeNumber(false)}>-</button>
    )

}


App.defaultProps = {
  num: 0
}
export default connect(
  (state) => ({  
    num: state.test.num 
  }),
  (dispatch) => ({ 
    testAction: bindActionCreators(testAction, dispatch), 
  })
)(App); 

์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฆฌ๋•์Šค ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ , ์ ์šฉํ•ด๋ดค๋‹ค.

์š”์•ฝ

  • store : ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋™์  ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ธฐ๋Š” ์ผ์ข…์˜ ์ค‘์•™ ์ €์žฅ์†Œ, ๋ณด๊ด€์†Œ(Globalized State)
  • action : ๋ฌด์—‡์„ ํ• ์ง€๋ฅผ ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„. counter๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด ์ˆซ์ž๋ฅผ 'Increment' ํ•œ๋‹ค ํ–ˆ์„ ๋•Œ ๋ฐ”๋กœ ๊ทธ 'increment' ๋ถ€๋ถ„
  • reducer : action ๊ฐ์ฒด๋ฅผ ๋ฐ›์•˜์„ ๋•Œ, ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐ”๊ฟ€์ง€ ๊ทธ ๋ฐฉ๋ฒ•์„ ์ •์˜ํ•˜๋Š” ๊ฐ์ฒด.
  • dispatch : action ๊ฐ์ฒด๋ฅผ reducer์— ์ „๋‹ฌํ•˜๋ฉด์„œ ์‹ค์งˆ์ ์œผ๋กœ ์ด ๊ณผ์ •์ด ์ผ์–ด๋‚˜๋„๋ก execute ํ•˜๋Š” ๋ถ€๋ถ„

์ƒํƒœ ๋ณ€๊ฒฝ์€ ์•„๋ž˜ ์ˆœ์„œ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

  1. dispatch๋กœ action ๊ฐ์ฒด๋ฅผ ๋ณด๋‚ธ๋‹ค.
  2. reducer๋Š” action ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.
  3. ๋ณ€๊ฒฝ๋œ ์ƒํƒœ๋Š” props๋กœ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

์ข€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ๋Š” Redux Toolkit์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.

๋‹ค์Œ ๋ฒˆ์—๋Š” ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ์— ์ „์—ญ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” data๋ฅผ state๋กœ ๊ด€๋ฆฌํ•˜๋„๋ก ๋ฆฌํŒฉํ† ๋ง์„ ํ•ด๋ด์•ผ๊ฒ ๋‹ค.

์ถœ์ฒ˜