DEV Community

Cover image for Understanding React Custom Hooks: A beginer's guide
Mitch Chimwemwe Chanza
Mitch Chimwemwe Chanza

Posted on • Originally published at blog.mitch.guru

Understanding React Custom Hooks: A beginer's guide

before we begin lets grow my twitter population ๐Ÿ˜„ follow me mitch_chanza

React hooks have become an integral part of modern React development, offering a way to manage stateful logic in functional components. While React provides several built-in hooks like useState and useEffect, creating custom hooks can further enhance code reusability and maintainability. In this blog, we'll explore the basics of custom React hooks, demystify their creation, and illustrate their practical use.

What are React Hooks?

React hooks are functions that allow you to use state and other React features in functional components. They were introduced in React 16.8 to enable functional components to manage state and side effects, which were previously exclusive to class components.

The most common built-in hooks are:

  1. useState: Manages component state.
  2. useEffect: Handles side effects in function components.
  3. useContext: Accesses the value of a React context.

Why Custom Hooks?

Custom hooks empower developers to encapsulate and reuse logic across different components. This can be particularly beneficial when dealing with complex state management, API calls, or any other shared functionality. By creating custom hooks, you can keep your code DRY (Don't Repeat Yourself) and make it more modular.

Anatomy of a Custom Hook

A custom hook is just a JavaScript function whose name starts with "use." By convention, this naming signals to both React and other developers that the function is a hook and should be used with the use keyword.

Let's create a simple custom hook called useCounter to manage a counter in our components.

// useCounter.js
import { useState } from 'react';

const useCounter = (initialValue = 0) => {
  const [count, setCount] = useState(initialValue);

  const increment = () => {
    setCount(count + 1);
  };

  const decrement = () => {
    setCount(count - 1);
  };

  return { count, increment, decrement };
};

export default useCounter;
Enter fullscreen mode Exit fullscreen mode

In this example, useCounter initializes a state variable count with an initial value (defaulting to 0) and provides functions to increment and decrement it.

Using the Custom Hook

Now that we've created our custom hook, let's use it in a component.

// CounterComponent.js
import React from 'react';
import useCounter from './useCounter';

const CounterComponent = () => {
  const { count, increment, decrement } = useCounter();

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
      <button onClick={decrement}>Decrement</button>
    </div>
  );
};

export default CounterComponent;
Enter fullscreen mode Exit fullscreen mode

By calling useCounter(), we get access to the state (count) and functions (increment and decrement) returned by our custom hook.

Rules of Hooks

When creating custom hooks, it's crucial to follow the "Rules of Hooks":

  1. Only call hooks at the top level: Don't call hooks inside loops, conditions, or nested functions. Always use them at the top level of your functional components.

  2. Only call hooks from React functions: Custom hooks should be called from functional components or other custom hooks, not from regular JavaScript functions.

Conclusion

Custom React hooks offer a powerful way to share logic between components and make your code more modular and maintainable. By creating custom hooks, you can encapsulate complex logic and promote reusability, enabling a more efficient and cleaner React codebase. Happy coding!

ooh yeah about the pupulation, remember to follow me ๐Ÿ˜„ mitch_chanza

Top comments (0)