Echo JS 0.11.0

<~>
tracker1 2994 days ago. link 1 point
redux-thunk[0] combined with fetch api[1][2] fits the bill pretty nicely.. even better with async functions for your action creators.

    export const myAction = (param) => async (dispatch, getState) => {
      try {
        dispatch(enableSpinner());
        const result = await fetch('foo');
        const data = await result.json();
        dispatch(dataLoaded(data)); // will disable spinner
      } catch(err) {
        dispatch(generalError(err)); // will disable spinner
      }
    }

[0] https://www.npmjs.com/package/redux-thunk
[1] https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
[2] https://www.npmjs.com/package/isomorphic-fetch

Replies

goshakkk 2994 days ago. link 1 point
Yes, I am giving a pointer to thunks in the end of the article. Still, thunks are no magic and are just a slightly nicer way to avoid explicitly passing in `dispatch` into action creators.
tracker1 2994 days ago. link 1 point
Absolutely... And I know there are other, more complex control flow plugins, but I find, similar to redux, that the thunks for action creators is easier to reason with while working on them.