惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
量子位
V
Visual Studio Blog
博客园 - Franky
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
罗磊的独立博客
小众软件
小众软件
V
V2EX
GbyAI
GbyAI
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
月光博客
月光博客
Recent Announcements
Recent Announcements
雷峰网
雷峰网
F
Fortinet All Blogs
M
MIT News - Artificial intelligence

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
React Hooks Explained: useMemo, useCallback, and useNavigate
Jayashree · 2026-06-12 · via DEV Community

Jayashree

React provides several powerful hooks that help developers optimize performance and manage navigation efficiently. In this blog, we'll explore useMemo, useCallback, and useNavigate with simple examples.

1. useMemo Hook

What is useMemo?

useMemo is a React Hook used to memoize a calculated value. It prevents expensive calculations from running on every render and only recalculates when dependencies change.

Syntax

const memoizedValue = useMemo(() => {
return expensiveCalculation(data);
}, [data]);

Why use useMemo?

  • Improves performance
  • Avoids unnecessary recalculations
  • Useful for expensive operations

Example

import React, { useState, useMemo } from "react";

function App() {
  const [count, setCount] = useState(0);
  const [text, setText] = useState("");

  const squaredValue = useMemo(() => {
    console.log("Calculating...");
    return count * count;
  }, [count]);

  return (
    <div>
      <h2>Count: {count}</h2>
      <h3>Square: {squaredValue}</h3>

      <button onClick={() => setCount(count + 1)}>
        Increment
      </button>

      <input
        type="text"
        placeholder="Type here"
        value={text}
        onChange={(e) => setText(e.target.value)}
      />
    </div>
  );
}

export default App;

Output

  • Calculation runs only when count changes.
  • Typing in the input field does not trigger recalculation.

2. useCallback Hook

What is useCallback?

useCallback is a React Hook used to memoize a function. It prevents a function from being recreated on every render.

Syntax

const memoizedFunction = useCallback(() => {
// function logic
}, [dependencies]);

Why use useCallback?

  • Prevents unnecessary function recreation
  • Improves performance
  • Useful when passing functions to child components

Example

Parent Component

import React, { useState, useCallback } from "react";
import Child from "./Child";

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

  const handleClick = useCallback(() => {
    console.log("Button Clicked");
  }, []);

  return (
    <div>
      <h2>Count: {count}</h2>

      <button onClick={() => setCount(count + 1)}>
        Increment
      </button>

      <Child onClick={handleClick} />
    </div>
  );
}

export default App;

Child Component

import React from "react";

const Child = React.memo(({ onClick }) => {
  console.log("Child Rendered");

  return (
    <button onClick={onClick}>
      Child Button
    </button>
  );
});

export default Child;

Output

  • Child component re-renders only when the callback changes.
  • Since useCallback memoizes the function, unnecessary re-renders are avoided.

Difference Between useMemo and useCallback

useMemo useCallback
Memoizes a value Memoizes a function
Returns calculated value Returns function
Used for expensive calculations Used for function optimization
Improves rendering performance Prevents unnecessary re-renders

Example

const value = useMemo(() => count * 2, [count]);

const handleClick = useCallback(() => {
  console.log("Clicked");
}, []);

3. useNavigate Hook

What is useNavigate?

useNavigate is a hook provided by the React Router library. It allows users to navigate between pages programmatically.

Installation

npm install react-router-dom

Syntax

const navigate = useNavigate();

navigate("/about");

Why use useNavigate?

  • Navigate to different pages
  • Redirect users after login
  • Navigate back and forward

Example
App.jsx

import { BrowserRouter, Routes, Route } from "react-router-dom";
import Home from "./Home";
import About from "./About";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;

Home.jsx

import { useNavigate } from "react-router-dom";

function Home() {
  const navigate = useNavigate();

  return (
    <div>
      <h1>Home Page</h1>

      <button
        onClick={() => navigate("/about")}
      >
        Go to About
      </button>
    </div>
  );
}

export default Home;

About.jsx

function About() {
  return <h1>About Page</h1>;
}

export default About;

Navigate Back

navigate(-1);

Moves to the previous page.

Navigate Forward

navigate(1);

Moves to the next page.

Replace Current History

navigate("/dashboard", { replace: true });

Prevents the user from returning to the previous page.

Conclusion

React hooks such as useMemo, useCallback, and useNavigate help developers build efficient and user-friendly applications.

useMemo → Memoizes values and avoids expensive recalculations.
useCallback → Memoizes functions and prevents unnecessary re-renders.
useNavigate → Enables programmatic navigation between routes.