Skip to content

Hooks in an API route handler #1869

Answered by juanpprieto
tylerpaige asked this question in Help
Discussion options

You must be logged in to vote

Hi @tylerpaige, thanks for the excellent write up.

As you mentioned, API functions are not react components. Think of them like a regular lambda function.

My advise is to modify your custom hook, so that the fetching logic is isolated from the hook logic. This way you end up with a utility and a hook — e.g fetchProduct and useProduct which uses fetchProduct internally.

This approach should help make testing easier and let you import fetchProduct inside the API route.

We do this in our internals with useShopQuery and queryShop (which gets passed as a param to the api handler)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@tylerpaige
Comment options

Answer selected by tylerpaige
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants