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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
小众软件
小众软件
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
G
Google Developers Blog
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
爱范儿
爱范儿
B
Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
博客园 - 叶小钗
aimingoo的专栏
aimingoo的专栏
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
博客园_首页
Google DeepMind News
Google DeepMind News
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
useMemo vs useCallback in React
Vidya · 2026-06-15 · via DEV Community
Cover image for useMemo vs useCallback in React

Vidya

React applications re-render whenever state or props change. In most cases, React handles re-rendering efficiently. However, in large applications with complex calculations or deeply nested components, unnecessary re-renders can impact performance.

To solve this problem, React provides two optimization hooks:

useMemo – Memoizes a value.
useCallback – Memoizes a function.

Although both are used for performance optimization, they serve different purposes.

What is useMemo?

useMemo is a React Hook that stores (memoizes) the result of a calculation and reuses it on future renders until one of its dependencies changes.

Definition

useMemo prevents React from recalculating a value every time the component renders. Instead, it recalculates only when the specified dependencies change.

Syntax

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

How it Works
=> React executes the function during the first render.
=> The returned value is stored in memory.
=> On subsequent renders, React checks the dependency array.
=> If dependencies haven't changed, React returns the stored value.
=> If dependencies change, React recalculates and stores the new value.

Example Without useMemo

function ProductList({ products }) {

  const sortedProducts = products.sort((a, b) =>
    a.price - b.price
  );

  return (
    <div>
      {sortedProducts.map(product => (
        <p key={product.id}>{product.name}</p>
      ))}
    </div>
  );
}

Problem

Every time the component renders:
=> The sorting operation runs again.
=> Even if the products array hasn't changed.
=> This wastes CPU resources.

Example With useMemo

import { useMemo } from "react";

function ProductList({ products }) {

  const sortedProducts = useMemo(() => {
    return [...products].sort((a, b) =>
      a.price - b.price
    );
  }, [products]);

  return (
    <div>
      {sortedProducts.map(product => (
        <p key={product.id}>{product.name}</p>
      ))}
    </div>
  );
}

Benefit

Now sorting happens only when the products array changes.
If another state changes in the component:
=> React re-renders.
=> Sorting is skipped.
=> The stored value is reused.

What is useCallback?

useCallback is a React Hook that stores a function and returns the same function reference across renders until dependencies change.

Definition

useCallback prevents React from creating a new function every time the component renders.

Syntax

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

Why Do We Need useCallback?

Consider this example:

function Parent() {

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

  const handleClick = () => {
    console.log("Button clicked");
  };

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

Whenever count changes:
--> Parent re-renders.
--> A new handleClick function is created.
--> Child receives a new function reference.
--> Child re-renders unnecessarily.

Example With useCallback

import { useCallback } from "react";" 

function Parent() {

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

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

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

Benefit
--> The same function reference is reused.
--> Child component doesn't re-render unnecessarily.
--> Performance improves.