3 ms·
Thunk is not about storing async state in the datastore, it's more akin to multithreading in my opinion. In backend, you might spin up a thread to do expensive
by silversmith 9y ago
Thunk is not about storing async state in the datastore, it's more akin to multithreading in my opinion.
In backend, you might spin up a thread to do expensive operation like API access, and that thread has it's own database connection, to read and persist data.
With thunk, you dispatch a thunk-ed action to do expensive operation like fetching data from backend, and that action has it's own access to redux in form of `dispatch` / `getState` arguments.
In the end, my component calls `fetchData` function upon mounting. And the logic dealing with making multiple requests due to paging, or re-trying on errors then lives its own life inside a thunk action, independent from the component that spawned it.
- eropple 9y agoHow do you test the code being run inside the Redux store? How do you mock your API client?
- silversmith 9y agoA thunk action `fetchData` would return a `(dispatch, getState) => {}` function. You call that with `getState` that would return your fixtures, and verify that `dispatch` gets called as expected. For API, I use jest and mock out imported functions.