Day 44 -  Understanding Custom Hooks

Photo by RetroSupply on Unsplash

Day 44 - Understanding Custom Hooks

Hi and welcome to Day 44 of my 100 Days of code challenge!πŸ’ͺ❀

In React, custom hooks are handy tools that help us reuse code effectively. These Hooks are JavaScript functions that can use other Hooks provided by React. Let's break down what custom hooks are, why they matter, and how to create them.πŸ‘‡

What are Custom Hooks ?

Custom hooks are like bundles of reusable logic that we can use across different parts of our app. They're great for sharing common functionalities. In React, we usually reuse two things:

  • A piece of UI and

  • A piece of logic.

While reusing UI with components is straightforward, reusing logic involves a bit more thinking.

Why Custom Hooks?

When we want to reuse logic in React, we need to check if it uses any React hooks. If it does, we can't just use a regular function; we need to create a custom hook to handle it properly.

Custom hooks allow us to reuse both state-related and non-visual logic. They help us organize our code better and make it easier to share complex logic between different components.

πŸ’‘
Custom Hooks let you share stateful logic, not state itself.

By encapsulating logic within custom hooks, developers can organize their code more efficiently. This separation of concerns leads to cleaner and more maintainable codebases. Developers can easily locate and manage specific pieces of logic, leading to better code readability and maintenance.

πŸ’‘
The main reason why you should be using Custom hooks is to maintain the concept of DRY (Don’t Repeat Yourself) in your React apps.

Creating Custom Hooks

A custom hook in React is just a JavaScript function that uses React hooks inside it. These functions can receive and return any relevant data, usually structured in objects or arrays.

Unlike regular functions or components, custom hooks must use React hooks to be recognized as such. For example, a custom hook might use useState and useEffect hooks to do something like fetching data from an API.

To make React recognize a function as a custom hook, its name must start with use. This naming rule is important for React to identify and treat the function correctly.

Example:

Let's say we have a custom hook called useFetch that handles fetching data from an API. By following the naming convention and using React hooks inside it, useFetch becomes a reusable tool for data fetching in different parts of our app.

πŸ’‘
Just like with regular functions, components, or effects, it's important for a custom hook to have a single clear purpose. It should focus on doing one specific thing well.

Tips for Building Custom Hooks

  1. Look for patterns in your code where you're doing the same thing repeatedly.

  2. When you find a pattern, create a new function for it and name it with "use" at the beginning. For example, if you're handling a fetch functionality, you might name your function "useFetch."

  3. Inside your custom hook, you can use any of React's built-in hooks if needed.

  4. Make sure your custom hook returns whatever information or functionality your component will need.

  5. Keep each custom hook focused on doing one thing really well.

Thank you for reading.

To be continued.πŸ’•πŸ’•

Β