redux-thunk
redux-thunk는 리덕스에서 비동기 작업을 처리 할 때 가장 많이 사용하는 미들웨어이다. 이 미들웨어를 사용하면 액션 객체가 아닌 함수를 디스패치 할 수 있다.
1. redux-thunk 설치 및 적용하기
redux-thunk를 설치
npm add redux-thunk
그 다음엔 redux-thunk를 index.js 에서 불러와서 applyMiddlewares를 통해 적용 한다.
index.js
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; import { createStore, applyMiddleware } from 'redux'; import { Provider } from 'react-redux'; import rootReducer from './modules'; import logger from 'redux-logger'; import { composeWithDevTools } from 'redux-devtools-extension'; import ReduxThunk from 'redux-thunk'; const store = createStore( rootReducer, // logger 를 사용하는 경우, logger가 가장 마지막에 와야한다. composeWithDevTools(applyMiddleware(ReduxThunk, logger)) ); // 여러개의 미들웨어를 적용 할 수 있다. ReactDOM.render( <React.StrictMode> <Provider store={store}> <App /> </Provider> </React.StrictMode>, document.getElementById('root') ); reportWebVitals();
2. 카운터 딜레이 하기
thunk 함수를 만들고, setTimeout를 사용하여 액션이 디스패치되는 것을 1초씩 딜레이시켜보자.
modules/counter.js
// 액션 타입 const INCREASE = 'INCREASE'; const DECREASE = 'DECREASE'; // 액션 생성 함수 export const increase = () => ({ type: INCREASE }); export const decrease = () => ({ type: DECREASE }); // getState를 쓰지 않는다면 굳이 파라미터로 받아올 필요 없습니다. export const increaseAsync = () => dispatch => { setTimeout(() => dispatch(increase()), 1000); }; export const decreaseAsync = () => dispatch => { setTimeout(() => dispatch(decrease()), 1000); }; // 초깃값 (상태가 객체가 아니라 그냥 숫자여도 상관 없습니다.) const initialState = 0; export default function counter(state = initialState, action) { switch (action.type) { case INCREASE: return state + 1; case DECREASE: return state - 1; default: return state; } }
increaseAsync와 decreaseAsync라는 thunk 함수를 만들었다. 이제 컨테이너 컴포넌트를 다음과 같이 수정한다.
containers/CounterContainer.js
import React from 'react'; import Counter from '../components/Counter'; import { useSelector, useDispatch } from 'react-redux'; import { increaseAsync, decreaseAsync } from '../modules/counter'; function CounterContainer() { const number = useSelector(state => state.counter); const dispatch = useDispatch(); const onIncrease = () => { dispatch(increaseAsync()); }; const onDecrease = () => { dispatch(decreaseAsync()); }; return ( <Counter number={number} onIncrease={onIncrease} onDecrease={onDecrease} /> ); } export default CounterContainer;
실행 결과

액션 디스패치가 1초 딜레이 되는 것을 볼 수 있다.
'개발 공부한 내용 정리 > React' 카테고리의 다른 글
react- thunk에서 라우터 연동하기 (0) | 2020.11.14 |
---|---|
react- redux-thunk로 프로미스 다루기 (0) | 2020.11.12 |
React- redux-logger 사용 및 미들웨어와 DevTools 함께 사용하기 (0) | 2020.11.07 |
React- 미들웨어 만들어보고 이해하기 (0) | 2020.11.06 |
React- 리덕스 미들웨어 실습을 위한 리덕스 프로젝트 준비 하기 (0) | 2020.11.05 |