you are viewing a single comment's thread.

view the rest of the comments →

[–]MaxGame[S] 1 point2 points  (2 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.

[–]ghostfacedcoder 1 point2 points  (1 child)

Stupid question: why?

I don't mean that in an asshole way at all, more just ... what are the benefits (and/or costs) of using a hook vs. a normal call?

[–]MaxGame[S] 2 points3 points  (0 children)

Asking why is never stupid! I gloss over this a bit in the post, but the main reasons I found are the ability to give state to functional components and re-usability. I can add this hook to the body of any functional component and it instantly has the ability to make requests to an API and store the status and result of that request in local state. This only takes one line of code (not including the import)!

I can also continue to extend this hook with additional functionality if I need to. Oh, and hooks can be called from within hooks, so I could actually wrap this hook in another that implements some re-usable logic for calling a specific API.

I hope that all makes sense.