By Saheb Sutradhar - Updated On 12-02-2025
React Hooks are functions that allow you to use state and other React features in functional components. Introduced in React 16.8, Hooks revolutionized the way developers build components by eliminating the need for class components while keeping components simpler and more readable.
Before Hooks, stateful logic was managed using class components. However, class components often led to complex code structures, making it harder to manage logic and reusability. Hooks solve these problems by enabling state and side effects in functional components, making React applications more maintainable and readable.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
The useEffect Hook is used for handling side effects, such as fetching data, subscriptions, and manual DOM manipulations.
import { useState, useEffect } from "react"; function Timer() { const [time, setTime] = useState(0); useEffect(() => { const interval = setInterval(() => { setTime((prevTime) => prevTime + 1); }, 1000); return () => clearInterval(interval); }, []); return <p>Timer: {time}s</p>; }
The useContext Hook allows components to consume values from a context without using the Consumer wrapper.
import { createContext, useContext } from "react"; const ThemeContext = createContext("light"); function ThemedButton() { const theme = useContext(ThemeContext); return <button style={{ background: theme === "dark" ? "black" : "white" }}>Click Me</button>; }
The useReducer Hook is an alternative to useState for handling more complex state logic.
import { useReducer } from "react"; function reducer(state, action) { switch (action.type) { case "increment": return { count: state.count + 1 }; case "decrement": return { count: state.count - 1 }; default: return state; } } function Counter() { const [state, dispatch] = useReducer(reducer, { count: 0 }); return ( <div> <p>Count: {state.count}</p> <button onClick={() => dispatch({ type: "increment" })}>+</button> <button onClick={() => dispatch({ type: "decrement" })}>-</button> </div> ); }
The useRef Hook is useful for accessing DOM elements directly or persisting values across renders without triggering re-renders.
import { useRef } from "react"; function InputFocus() { const inputRef = useRef(null); const focusInput = () => { inputRef.current.focus(); }; return ( <div> <input ref={inputRef} type="text" /> <button onClick={focusInput}>Focus Input</button> </div> ); }
The useMemo Hook memoizes a computation to avoid unnecessary recalculations.
import { useMemo} from "react"; function ExpensiveCalculation({ num }) { const squared = useMemo(() => num * num, [num]); return <p>Squared: {squared}</p>; }
The useCallback Hook memoizes functions to prevent unnecessary re-creations.
import { useCallback} from "react"; function Button({ handleClick }) { return <button onClick={handleClick}>Click me</button>; }
Runs synchronously after all DOM mutations.
Allows customizing the instance value exposed when using ref.
Used to display custom hooks debugging information.
Delays a state update to prevent blocking rendering.
Manages transitions between states for better user experience.
Generates unique IDs for accessibility and dynamic elements.
Subscribes to external data sources with consistent updates.
React Hooks make functional components more powerful by allowing state, side effects, and context handling without the need for class components. They promote cleaner, more reusable code and have become the standard for building modern React applications. If you have not already started using Hooks, now is the perfect time to dive in.
Trending Posts
What Are the Hooks in React...
Create Input Floating Label in CSS and HTML...
CSS Card Hover Effects: Make Your Website Stand Ou...
Create a Rotate Image Gallery with HTML and CSS...
CSS Hover Effect | Web Development...
How to create MongoDB Free cloud Database - Atlas ...
Learn how to create CSS Button RGB Animation...
Create Responsive Sidebar with React JS and tailwi...
Build a JavaScript Carousel Slider With Example...
How to Disable the Submit Button in Formik...
codelearningpoint © 2024