This content originally appeared on DEV Community and was authored by Turjo Chowdhury
The custom hook is one of the core concepts of react it helps to make the code organized and reusable. We can create a hook with the help of javascript and share the logic from anywhere in the application. When we want to share logic between other components, we can make it to a separate function. Custom hook always starts with “use”. Custom hooks are used to obey the DRY principle.
An example of custom hooks is like- imagine we have to share the user data into many components so if the user logged in. so here we can create a custom hook with all the login functionality and states with the data and return them from the function so other components can access it.
So all we have to do is just import and call the function from any component.
so, imagine we have to share the login function and also the information of a user into many components once the user is logged in.
First of all, we should use the “use” word in the file name it is a convention. Like useLogin then we can write all the logic of a login functionalities into that file and return the states or functions. Then we must export the main function so we can import it from anywhere.
This content originally appeared on DEV Community and was authored by Turjo Chowdhury
Turjo Chowdhury | Sciencx (2021-12-22T17:39:49+00:00) CUSTOM HOOK concept for beginners. Retrieved from https://www.scien.cx/2021/12/22/custom-hook-concept-for-beginners/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.