Summary
In this chapter, we first learned how to extract custom Hooks from existing code in our blog app. We defined the Theme Hook to easily access the context, then we defined the User Hook, which manages user state and provides functions to register/login/logout. Then, we created API Hooks and a more advanced Hook for the debounced history state functionality. Finally, we learned about writing tests for our custom Hooks using Vitest and the React Testing Library.
Knowing when and how to extract custom Hooks is a very important skill in React development. In a larger project, you are probably going to define many custom Hooks, specifically tailored to your project’s needs. Custom Hooks can also make it easier to maintain the application, as we only need to adjust functionality in one place. Testing custom Hooks is very important, because if we refactor our custom Hooks later on, we want to make sure that they still work properly.
In the next chapter, we are going...