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 comp…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » CUSTOM HOOK concept for beginners." Turjo Chowdhury | Sciencx - Wednesday December 22, 2021, https://www.scien.cx/2021/12/22/custom-hook-concept-for-beginners/
HARVARD
Turjo Chowdhury | Sciencx Wednesday December 22, 2021 » CUSTOM HOOK concept for beginners., viewed ,<https://www.scien.cx/2021/12/22/custom-hook-concept-for-beginners/>
VANCOUVER
Turjo Chowdhury | Sciencx - » CUSTOM HOOK concept for beginners. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/22/custom-hook-concept-for-beginners/
CHICAGO
" » CUSTOM HOOK concept for beginners." Turjo Chowdhury | Sciencx - Accessed . https://www.scien.cx/2021/12/22/custom-hook-concept-for-beginners/
IEEE
" » CUSTOM HOOK concept for beginners." Turjo Chowdhury | Sciencx [Online]. Available: https://www.scien.cx/2021/12/22/custom-hook-concept-for-beginners/. [Accessed: ]
rf:citation
» CUSTOM HOOK concept for beginners | Turjo Chowdhury | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.