React Hooks are a core feature that changed how developers write React applications. Before Hooks were introduced, state and lifecycle features were only available in class components. Hooks allow these features to be used inside functional components, making code simpler and more consistent.
At a fundamental level, Hooks are just JavaScript functions provided by React. They let you “hook into” React features such as state, side effects, context, and references.
Basic Hook Example
The most commonly used Hook is useState, which allows a functional component to store and update state.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}
Here, useState creates a state variable and a function to update it. When the state changes, React re-renders the component automatically.
Another fundamental Hook is useEffect, which is used to perform side effects such as API calls, subscriptions, or timers.
Example:
import { useEffect } from "react";
function Example() {
useEffect(() => {
console.log("Component mounted");
}, []);
return <p>Hello</p>;
}
The empty dependency array ensures the effect runs only once when the component mounts.
Why Hooks Are Important
Hooks eliminate the need for complex class-based syntax, reduce boilerplate code, and make it easier to reuse logic across components. They encourage cleaner and more readable code while maintaining full control over component behavior.
Rules of Hooks
React enforces two strict rules for using Hooks. These rules ensure that Hooks behave consistently and correctly across renders.
Rule 1: Only Call Hooks at the Top Level
Hooks must be called at the top level of a React functional component or a custom Hook. They must not be called inside loops, conditions, or nested functions.
Incorrect usage:
if (isLoggedIn) {
useState(true);
}
Correct usage:
const [isLoggedIn, setIsLoggedIn] = useState(true);
Calling Hooks at the top level ensures React calls them in the same order on every render.
Rule 2: Only Call Hooks from React Functions
Hooks can only be called from React functional components or custom Hooks. They must not be called from regular JavaScript functions.
Incorrect usage:
function helper() {
useState(0);
}
Correct usage:
function useCustomHook() {
const [value, setValue] = useState(0);
return value;
}
Real-World Scenario
In real-world applications, Hooks are used to manage form state, fetch data from APIs, track user interactions, and share logic across components. For example, a custom Hook can be created to handle form validation logic and reused across multiple forms.
Important Notes and Best Practices
Always follow the Rules of Hooks to avoid unpredictable behavior. Use custom Hooks to extract reusable logic. Keep Hooks focused on a single responsibility to improve readability and maintainability.
In summary, React Hooks provide a modern, powerful way to manage state and side effects in functional components. Understanding their fundamentals and strictly following the Rules of Hooks is essential for building stable, maintainable, and professional React applications.