[フレーム]

React useCallback Hook


The useCallback Hook is used to memoize a callback function.

Memoizing a function means caching the result of a function so that it does not need to be recalculated.

The useCallback function only re-executes when one of its dependencies changes value.

This allows us to isolate resource intensive functions so that they will not automatically run on every render.

The useCallback and useMemo Hooks are similar:

useMemo returns a memoized value.

useCallback returns a memoized function.

Learn more about useMemo in the useMemo chapter.


Syntax

The useCallback Hook accepts two arguments.

useCallback(callback, dependencies)

callback: The function that you want to memoize.

dependencies: An array of dependencies for the callback function. The memoized callback will only change if one of these dependencies has changed.

Example:

Without useCallback:

//Without useCallback:
import React, { useState } from 'react';
import { createRoot } from 'react-dom/client';
// Child component that receives a function prop
const Button = React.memo(({ onClick, text }) => {
 alert(`Child ${text} button rendered`);
 return <button onClick={onClick}>{text}</button>;
});
// Parent component without useCallback
function WithoutCallbackExample() {
 const [count1, setCount1] = useState(0);
 const [count2, setCount2] = useState(0);
 // This function is recreated on every render
 const handleClick1 = () => {
 setCount1(count1 + 1);
 };
 const handleClick2 = () => {
 setCount2(count2 + 1);
 };
 alert("Parent rendered");
 return (
 <div>
 <h2>Without useCallback:</h2>
 <p>Count 1: {count1}</p>
 <p>Count 2: {count2}</p>
 <Button onClick={handleClick1} text="Button 1" />
 <Button onClick={handleClick2} text="Button 2" />
 </div>
 );
}
createRoot(document.getElementById('root')).render(
 <WithoutCallbackExample />
); 

Run Example »



Try running the example above and click the buttons.

You will notice that all three components (Parent, Button 1 and Button 2) re-render each time you click the buttons.

This can be avoided by using the useCallback hook.

By using the useCallback hook, we can memoize the functions and only recreate them when their dependencies change.

When clicking Button 1, only Parent and Button 1 should re-render, and when clicking Button 2, only Parent and Button 2 should re-render:

Example:

With useCallback:

//With useCallback:
import React, { useState, useCallback } from 'react';
import { createRoot } from 'react-dom/client';
// Child component that receives a function prop
const Button = React.memo(({ onClick, text }) => {
 console.log(`${text} button rendered`);
 return <button onClick={onClick}>{text}</button>;
});
// Parent component with useCallback
function WithCallbackExample() {
 const [count1, setCount1] = useState(0);
 const [count2, setCount2] = useState(0);
 // These functions are memoized and only recreated when dependencies change
 const handleClick1 = useCallback(() => {
 setCount1(count1 + 1);
 }, [count1]);
 const handleClick2 = useCallback(() => {
 setCount2(count2 + 1);
 }, [count2]);
 console.log("Parent rendered");
 return (
 <div>
 <h2>With useCallback:</h2>
 <p>Count 1: {count1}</p>
 <p>Count 2: {count2}</p>
 <Button onClick={handleClick1} text="Button 1" />
 <Button onClick={handleClick2} text="Button 2" />
 </div>
 );
}
createRoot(document.getElementById('root')).render(
 <WithCallbackExample />
); 

Run Example »



New

W3Schools Adventure App

Coding fundamentals as a game. Bite-sized lessons and challenges.

Ready to start your journey? Your streak is waiting.
+60 XP
W3Schools Adventure tutor
W3Schools Adventure map
×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

AltStyle によって変換されたページ (->オリジナル) /