From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Custom hook design principles

Custom hook design principles

Welcome back, let's review some of the design principles and map it on the custom hook learning that we have. So there are common design principles and the most important is single responsibility principle. It states that one specific function, a class or a module should have one common responsibility and one single responsibility only. For example, if you are writing down an increment function that it should only be responsible for incrementing into the count, not making any other change or resetting it. Similarly, from this principle, we can have a custom hook and this hook should do one thing well enough. For example, we can define a hook for useFetch which allows us to call fetch APIs. In this useFetch hook, we can have input parameters such as the API path, the method we want to want to call, whether it's a POST or it's a GET method, or what is the body of that specific API call, and it can return back the result to us. So rather than writing down four to five lines of code, we…

Contents