Open JaeYeopHan opened 5 years ago
redux-saga에서는 간단히 순서를 바꾸고 reducer들을 만들어두면 금방 적용해볼 수 있다.
function* sagaFunction(action: Action<IRequest>) {
// api call이 성공했다는 낙관적인 가정하에 success action을 dispatch 한다.
// 이렇게 되면 api response를 기다리지 않고 UI 상태를 바로 바꿔줄 수 있다.
yield put(asyncAction.success(action.payload))
try {
yield call(apiRequestFunction, action.payload)
} catch (e) {
// 에러 발생 시, 미리 바꿨던 UI 상태를 롤백한다.
yield put(asyncAction.failure(action.payload))
} finally {
}
}
Optimistic UI
What?
API를 호출하고 응답이 오기 전까지 기다렸다가 상태를 바꾸면 delay를 주는 느낌이 발생한다. 우선 상태값을 변경한 후 api 호출을 보낸다. api 호출이 실패할 경우 상태를 원복한다.
When?
ex) 즐겨찾기 추가 버튼
Problems
(TBD)
Reference
https://uxdesign.cc/the-optimistic-ui-with-react-f1420e317d54