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 example and usage

Custom hook example and usage

Hello and welcome back. In this video, we'll see a basic custom hook example and how we can use that custom hook in our other components. So to understand this, let's continue forward with the very famous counter example. So far we have done this code so many times in contrast with learning about some hooks and learning about components. Now you might have seen that we have been writing down this logic for setting the counter, assigning a state for this along with the initial value and then writing down the functions for incrementing, decrementing and resetting it. We can create a custom hook like useCounter for this purpose and all the other components which needs the counter example logic, we can call the custom hook to have the counter functionality inside that component. So there is only one rule. As I told you, the name should start with use keyword. So over here we have the use counter hook. This is just like a simple JavaScript function. As you can see, it can use the other…

Contents