React Hooks

Introduction

React Hooks are one of the most important features introduced in React. Before Hooks were introduced, developers mainly used class components for handling:

  • State
  • Lifecycle methods
  • Side effects

However, class components were often complex and difficult to manage in large applications. To solve these problems, React introduced Hooks in React 16.8.

Hooks allow developers to use React features such as state and lifecycle functionality inside functional components.

What are React Hooks?

React Hooks are special functions that allow functional components to use React features like:

  • State
  • Lifecycle methods
  • Context
  • Refs

Before Hooks:

  • Functional components were simple UI components
  • State management required class components

After Hooks:

  • Functional components became powerful and fully featured

Why Hooks are Used in React?

Hooks were introduced to solve several problems in class components.

Problems with Class Components

  • Complex syntax
  • Difficult lifecycle methods
  • Repetitive logic
  • Hard-to-manage state
  • Confusing this keyword

Hooks simplify all these issues.

Benefits of Hooks

  • Simpler code
  • Better readability
  • Reusable logic
  • Easier state management
  • Better performance optimization

Syntax of React Hook

Basic Hook example using useState:


import { useState } from "react";
function App() {
 const [count, setCount] = useState(0);
 return (
   <div>
     <h1>{count}</h1>
     <button onClick={() => setCount(count + 1) } > Increment </button>
   </div>
 );
}
export default App;

Understanding the Syntax

useState

A Hook used for state management.

count

Current state value.

setCount

Function used to update state.

Rules of Hooks

React Hooks follow special rules.

1. Only Call Hooks at Top Level

Correct:


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

Wrong:


if (true) {
 useState(0);
}

2. Only Use Hooks Inside React Functions

Hooks can be used:

  • Inside functional components
  • Inside custom Hooks

Not inside:

  • Regular JavaScript functions
  • Loops
  • Conditions

Common React Hooks

React provides multiple built-in Hooks.

Main Hooks:

  • useState
  • useEffect
  • useContext
  • useRef
  • useMemo
  • useCallback

1. useState Hook

useState is used for managing component state.

Example


import { useState } from "react";
function Counter() {
 const [count, setCount] = useState(0);
 return (
   <button onClick={() => setCount(count + 1) } >
     {count}
   </button>
 );
}

Output

Counter increases on button click.

2. useEffect Hook

useEffect handles side effects such as:

  • API calls
  • Timers
  • DOM updates

Example


import { useEffect } from "react";
function App() {
 useEffect(() => {
   console.log("Component Rendered");
 }, []);
 return <h1>Hello</h1>;
}

3. useContext Hook

useContext shares data globally without prop drilling.

Example


const value = useContext(UserContext);

4. useRef Hook

useRef accesses DOM elements directly.

Example


const inputRef = useRef();

5. useMemo Hook

useMemo optimizes expensive calculations.

Example


const result = useMemo(() => {
 return calculate();
}, []);

6. useCallback Hook

useCallback memoizes functions.

Example


const memoizedFunction =
 useCallback(() => {
   doSomething();
 }, []);

Difference Between Hooks and Class Components

Feature Hooks Class Components
Syntax Simple Complex
State Handling useState this.state
Lifecycle Methods useEffect lifecycle methods
Reusability Better Limited
Modern Usage Recommended Less Common

Why Hooks are Popular?

Hooks became popular because they:

  • Reduce code complexity
  • Improve readability
  • Remove this keyword confusion
  • Support reusable logic

Real-Life Example of React Hooks

Suppose you are building an e-commerce website.

Hooks help manage:

  • Product state
  • Shopping cart
  • User authentication
  • API requests
  • Theme switching

Without Hooks, managing these features becomes difficult.

Custom Hooks in React

Developers can create their own Hooks.

Custom Hooks help reuse logic.

Example


import { useState } from "react";
function useCounter() {
 const [count, setCount] = useState(0);
 function increment() {
   setCount(count + 1);
 }
 return {
   count,
   increment
 };
}

Benefits of Custom Hooks

  • Reusable logic
  • Cleaner code
  • Better maintainability

Advantages of React Hooks

Feature Benefit
Simpler Syntax Easier learning
State Management Better component control
Reusability Reusable logic
Cleaner Code Reduced complexity
Functional Components Modern development

Common Mistakes in React Hooks

1. Calling Hooks Inside Conditions

Wrong:


if (true) {
 useState(0);
}

Correct:


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

2. Forgetting Dependency Array in useEffect

Wrong:


useEffect(() => {
 console.log("Hello");
});

This runs after every render.

3. Updating State Incorrectly

Wrong:


count = count + 1;

Correct:


setCount(count + 1);

4. Overusing useEffect

Not every operation requires useEffect.

5. Using Hooks Outside Components

Hooks only work inside React functions.

Best Practices for React Hooks

  • Use functional components
  • Keep Hooks at top level
  • Use custom Hooks for reusable logic
  • Keep state minimal
  • Avoid unnecessary effects
  • Use proper dependency arrays

Real-World Applications of React Hooks

Hooks are heavily used in:

  • Social media apps
  • E-commerce websites
  • Banking applications
  • Admin dashboards
  • Chat applications

Examples:

  • Login systems
  • API handling
  • Theme switching
  • Notifications
  • Search filters

React Hooks vs Traditional JavaScript Functions

Feature React Hooks JavaScript Functions
React Features Supported Not Supported
State Management Yes No
Lifecycle Access Yes No
Re-rendering Supported No

React Hooks – Interview Questions

Q 1: What are React Hooks?
Ans: React Hooks are functions introduced in React 16.8 that allow function components to use state, lifecycle methods, and other React features without writing class components.
Q 2: Why were Hooks introduced in React?
Ans: Hooks were introduced to simplify state management, reduce code complexity, and improve reusability of logic across components.
Q 3: Can Hooks be used in class components?
Ans: No, Hooks can only be used inside function components or custom Hooks.
Q 4: What are the rules of Hooks?
Ans: Hooks must be called at the top level and only inside React function components or custom Hooks.
Q 5: Name some commonly used Hooks.
Ans: useState, useEffect, useContext, useMemo, and useCallback.

React Hooks – Objective Questions (MCQs)

Q1. What are React Hooks?






Q2. Which version of React introduced Hooks?






Q3. Which of the following is NOT a built-in React Hook?






Q4. Hooks can be used in which components?






Q5. What is the main rule of Hooks?






Conclusion

React Hooks are one of the most powerful features in modern React development. They allow developers to use state, lifecycle methods, refs, and context inside functional components without using class components.

Hooks make React applications:

  • Cleaner
  • Simpler
  • More reusable
  • Easier to maintain

Continue Learning React