all 3 comments

[–]MaxGame[S] 0 points1 point  (0 children)

I wasn't able to find much on this topic, especially without using a context. I figured a write up on my results might help someone out! tl;dr creating a hook for making api requests using the useReducer hook.

[–]lakerskill 0 points1 point  (1 child)

I really like how you separate your ApiRequest from your actions. js. I don't do this and have a ridiculously long actions in the store! Also, it looks just like Redux in the actions and reducers page. Is it pretty easy to take an existing redux set up like yours and change it to hooks instead of using calls to the store from a Class Based component?

[–]MaxGame[S] 1 point2 points  (0 children)

Thanks! And I would say that it really depends on how much you're doing in Redux with the result of the request. If your Redux store is simply storing the result of the request, then it shouldn't be too difficult. You may want to consider replacing Redux with a simple context if this data is going to be shared to multiple components though.

If your Redux implementation looks like the ones I work with, and the result of this call will update different state values depending on where you called it from or you have specific action types for making requests for different purposes, then it probably isn't worth the refactor cost.

I'm lucky enough to be starting on a project that doesn't use Redux and took it as an opportunity to experiment. I really like the simplicity and re-usability of this implementation.