An Interest In:
Web News this Week
- April 13, 2024
- April 12, 2024
- April 11, 2024
- April 10, 2024
- April 9, 2024
- April 8, 2024
- April 7, 2024
CUSTOM HOOK concept for beginners
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.
Original Link: https://dev.to/turjoc120/custom-hook-concept-for-beginners-4hk2
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To