모듈을 다 수정했다면 리덕스 스토어에 redux-saga를 적용하겠습니다.
먼저 루트 사가를 만드세요.
modules/index.js
import { combineReducers } from 'redux'; import users, { usersSaga } from './users'; import { all } from 'redux-saga/effects'; export function* rootSaga() { yield all([usersSaga()]); } const rootReducer = combineReducers({ users }); export default rootReducer;
다음으로 스토어를 생성할 때 미들웨어를 적용하세요.
src/index.js
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import * as serviceWorker from './serviceWorker'; import { BrowserRouter } from 'react-router-dom'; import { createStore, applyMiddleware } from 'redux'; import { Provider } from 'react-redux'; import thunk from 'redux-thunk'; import createSagaMiddleware from 'redux-saga'; import rootReducer, { rootSaga } from './modules'; const sagaMiddleware = createSagaMiddleware(); const store = createStore( rootReducer, window._ _PRELOADED_STATE_ _, // 이 값을 초기 상태로 사용함 applyMiddleware(thunk, sagaMiddleware) ); sagaMiddleware.run(rootSaga); ReactDOM.render( <Provider store={store}> <BrowserRouter> <App /> </BrowserRouter> </Provider>, document.getElementById('root') ); serviceWorker.unregister();