You can find a summary of the code updates in this pull request . Read on for explanation.
Why custom hooks?
We use custom hooks for the same reasons we use functions – either because
- we want to re-use the same logic over and over, or
- we want to abstract the details of the logic away from where the logic is called.
In this case, we’re creating a custom hook for the second reason. We don’t intend to re-use this logic anywhere else, but it’s nice to have the logic tucked away in its own file. Someone reading the Home
component doesn’t need all those details up front, and the complicated logic gets in the way of understanding how the component works at a high level.
1. Create custom hook file
We’ll create a custom hook file in /src/component/hooks/use-quote-styles.ts. (You’ll need to create the folder as well, since this is our first hook and the hooks folder doesn’t exist yet.)
Within use-quote-styles.ts, we can declare and export the custom hook. We’ll call it useQuoteStyles
, since (all hooks start with use ).
use-quote-styles.ts
2. Move the logic
Now we’ll move all the logic (including state) from the Home
component in page.tsx to useQuoteStyles
, and return all state plus the handleClick
function.
A couple notes:
- We’ll need to move the
Status
type over, too, since it’s used in this file now and not page.tsx. - I used the name
fetchQuoteStyles
for thehandleClick
function, becausehandleClick
doesn’t mean as much when it’s not defined right next to the button whose click is being handled.
use-quote-styles.ts
3. Call the hook
Now we need to call the hook from the Home
component, destructuring the object from the return value. We’ll also need to update the onClick
value for Button
.
page.tsx
Up next
Now that the logic is all nicely in place, we’ll update the UI in the next workshop to reflect the loading and error states.