codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

19 / 61 topics
18Context API Basics19Hooks: Introduction and Overview20Using the useState Hook21Using the useEffect Hook22Creating Custom Hooks23Memoization in React
Tutorials/React.js/Hooks: Introduction and Overview
⚛️React.js

Hooks: Introduction and Overview

Updated 2026-04-20
3 min read

Hooks: Introduction and Overview

Introduction

React, a popular JavaScript library for building user interfaces, has evolved significantly over the years. One of its most significant additions is the introduction of Hooks, which were released in React 16.8. Before Hooks, managing state and lifecycle methods required class components. However, with Hooks, functional components can now use state and other React features without converting them into class components.

This tutorial will provide a comprehensive introduction to Hooks, their purpose, how they work, and some best practices for using them effectively in your React applications.

What are Hooks?

Hooks are special functions that let you “hook into” React state and lifecycle features from function components. They allow you to use state and other React features without writing a class.

Types of Hooks

React provides several built-in Hooks, which can be categorized into two main types:

  1. State Hooks: These Hooks manage the component's state.
  2. Effect Hooks: These Hooks handle side effects in function components.

State Hook: useState

The useState Hook allows you to add React state to function components. It returns a pair: the current state value and a function that lets you update it.

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

In this example, useState is used to create a state variable count, initialized with 0. The setCount function is used to update the state.

Effect Hook: useEffect

The useEffect Hook lets you perform side effects in function components. It serves the same purpose as componentDidMount, componentDidUpdate, and componentWillUnmount in class components.

import React, { useState, useEffect } from 'react';

function Example() {
  const [count, setCount] = useState(0);

  // Similar to componentDidMount and componentDidUpdate:
  useEffect(() => {
    // Update the document title using the browser API
    document.title = `You clicked ${count} times`;
  });

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

In this example, useEffect is used to update the document title whenever the count state changes.

Rules of Hooks

To ensure that Hooks work correctly and predictably, React has two primary rules:

  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 React function.

  2. Only Call Hooks from React Functions: Only call Hooks from React function components or custom Hooks. Don't call Hooks from regular JavaScript functions.

Custom Hooks

Custom Hooks are a way to reuse logic with Hooks. A custom Hook is just a function whose name starts with use and that may call other Hooks.

import { useState, useEffect } from 'react';

function useFriendStatus(friendID) {
  const [isOnline, setIsOnline] = useState(null);

  useEffect(() => {
    function handleStatusChange(status) {
      setIsOnline(status.isOnline);
    }

    ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
    return () => {
      ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
    };
  });

  return isOnline;
}

In this example, useFriendStatus is a custom Hook that encapsulates the logic for subscribing to a friend's status.

Best Practices

  1. Avoid Repeated Effects: If you want to perform an effect only once after the initial render, you can pass an empty array ([]) as the second argument to useEffect. This tells React that your effect doesn't depend on any values from props or state, so it never needs to re-run.

  2. Use Multiple State Variables: If your component needs to track multiple pieces of state, consider using multiple state variables instead of a single object. This makes the code more readable and easier to manage.

  3. Avoid Unnecessary Dependencies: In useEffect, include only those values that are actually used inside the effect. Avoid including unnecessary dependencies, as this can lead to performance issues.

  4. Name Custom Hooks Appropriately: When creating custom Hooks, name them in a way that clearly describes their purpose. This makes your code more readable and maintainable.

Conclusion

Hooks provide a powerful way to manage state and side effects in functional components, making React applications easier to write and understand. By following the rules of Hooks and applying best practices, you can build robust and efficient React applications.

In the next sections, we will explore more advanced topics related to Hooks, including how to use them with forms, context, and other complex scenarios.


PreviousContext API BasicsNext Using the useState Hook

Recommended Gear

Context API BasicsUsing the useState Hook