반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 시스템설계면접팁
- 가상면접2장
- 시스템설계
- 전략패턴
- awss3
- git squash
- cypressBDD
- git commit 협업
- 테스트코드책
- 리팩터링2판테스트
- 시스템설계면접
- FirebaseAnalytics
- s3이미지다운로드됨
- 헤드퍼스트전략패턴
- formik react-query submitting not working
- 리팩토링2판4장
- 시스템설계방법
- 시스템설계면접예시
- 가상면접3장
- gitsquash
- Git commit 합치기
- git commit merge
- cypress React
- react
- formik submitting not working
- react-ga
- 가상면접으로대규모시스템
- file not found Error
- 리액트구글애널리틱스
- 디자인패턴
Archives
- Today
- Total
mingg IT
[React] 미들웨어 redux-saga 예제 (takeEvery, TaskLatest) 본문
redux-saga
기존 요청을 취소 처리해야 할 때 (불필요한 중복 요청 방지)
특정 액션이 발생했을 때 다른 액션을 발생시키거나, API요청 등 리덕스와 관계없는 코드를 실행할 때
웹소켓을 사용할 때
API 요청 실패 시 재요청을 해야할 때
yarn add redux-saga
counter.js
import { createAction, handleActions } from "redux-actions";
import { delay, put, takeEvery, takeLatest } from "redux-saga/effects";
const INCREASE = "counter/INCREASE";
const DECREASE = "counter/DECREASE";
export const increase = createAction(INCREASE);
export const decrease = createAction(DECREASE);
const INCREASE_ASYNC = "counter/INCREASE_ASYNC";
const DECREASE_ASYNC = "counter/DECREASE_ASYNC";
export const increaseAsync = createAction(INCREASE_ASYNC, () => undefined);
export const decreaseAsync = createAction(DECREASE_ASYNC, () => undefined);
function* increaseSaga() {
yield delay(1000);
yield put(increase());
}
function* decreaseSaga() {
yield delay(1000);
yield put(decrease());
}
export function* counterSaga() {
yield takeEvery(INCREASE_ASYNC, increaseSaga);
yield takeLatest(DECREASE_ASYNC, decreaseSaga);
}
const initialState = 0;
const counter = handleActions(
{
[INCREASE]: (state) => state + 1,
[DECREASE]: (state) => state - 1,
},
initialState
);
export default counter;
DECREASE 의 경우 takeLatest로 되어있기 때문에 여러 액션이 중첩되어 dispatch될 경우 기존것들은 무시하고 가장 마지막 액션만 처리한다.
'FrontEnd' 카테고리의 다른 글
[React] redux-action 사용하기 (typeScript) (0) | 2021.11.30 |
---|---|
[React] ref를 활용하여 스크롤 조절하기 (0) | 2021.10.28 |
[React] 리액트 미들웨어 redux-thunk 예제 (0) | 2021.08.02 |
[React] 콘솔 로그에 색상 입히기, redux-logger 사용하기 (0) | 2021.08.02 |
[React] 크롬 개발자 도구를 통한 성능 모니터링 및 최적화 (0) | 2021.07.23 |
Comments