Blogged Answers: Reasons to Use Thunks
markerikson opened this issue · comments
Mark Erikson commented
Mark Erikson commented
Original author: Karen Grigoryan @kapral18
Original date: 2020-05-19T21:19:27Z
Thanks for great article.
Couple of remarks if I may:
On the other hand, I've seen a number of folks complain that testing sagas often degenerates
to effectively testing implementation details of the saga, vs the final result.
It's absolutely possible to test the behavior instead of implementation check
https://redux-saga.js.org/d...
or even better
https://github.com/jfairban....
So testability is still strong with redux-saga.
----------------
Thunks can return a value such as a promise to the site that dispatched them,
allowing you to do things like executing component logic after a fetch has completed
How about this?
// Component.tsx
const Component = ({fetchSomethingSaga, url}) => {
useEffect(() => {
fetchSomethingSaga(url, {
onSuccess: console.log,
onError: console.error
})
}, [fetchSomethingSaga, url]);
return null;
}
// fetchSomethingSaga.tsx
export function* fetchSomethingSaga(action) {
try {
const response = yield call(fetchApi, action.url)
//...
yield call(action.onSuccess, response)
} catch(err) {
yield call(action.onError, err)
}
}
export default takeLatest(
'FETCH_SOMETHING_SAGA',
fetchSomethingSaga,
)