Boosting React Performance: useCallback vs. useMemo Hooks
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Boosting React Performance: useCallback vs. useMemo Hooks

Boosting React Performance: useCallback vs. useMemo Hooks

React is a popular JavaScript framework for front-end development that has made developers’ lives easier by introducing component-driven development.

Before React 16, components were usually defined with Class and had different lifecycle methods. React 16 promoted the creation of functional components and introduced the following hooks:

  • useEffect for handling the component’s lifecycle.
  • useState for state management.

These hooks are necessary because re-renders and re-computations are costly operations, so minimizing them improves the application’s performance.

React has recently introduced two hooks, useCallback and useMemo. Like the useEffect and useState hooks, these are potent tools that can help developers optimize the performance of their applications by reducing unnecessary re-renders and re-computations.

In this blog, we will delve into these hooks, exploring their functionality and examining how they work. We will also discuss the appropriate use cases for these hooks, providing practical examples to help you understand when and where to use them in your projects. Let’s get started!

Syncfusion React UI components are the developers’ choice to build user-friendly web applications. You deserve them too.

useCallback hook

Syntax

const computedFn = useCallback(()=>{
  doSomething(dependencies);
}, [dependencies]);

In the previous code example, doSomething(dependencies); will be returned and stored in the computedFn variable. This only occurs when the dependencies change, and useCallback will provide the new doSomething(dependencies); value.

This is useful when you want a function to avoid re-rendering whenever the component changes.

For example, we will use the debounce function on the input field to get the data only when the user stops writing for a specific amount of time.

import React, { useState } from "https://esm.sh/react@18.2.0";
import ReactDOM from "https://esm.sh/react-dom@18.2.0";
const App = () => {
  const [text, setText] = useState("");
  const debounce = (func, delay) => {
	let inDebounce;
	return function () {
  	const context = this;
  	const args = arguments;
  	clearTimeout(inDebounce);
  	inDebounce = setTimeout(() => func.apply(context, args), delay);
	};
  };
  const makeApiCall = () => {
	console.log(text, " Making an API call");
  };
  const debouncedApiCall = debounce(makeApiCall, 500);
  const onChangeHandler = (e) => {
	setText(e.target.value);
	debouncedApiCall();
  };
  return (
	<div>
  	<input type="text" onChange={onChangeHandler} value={text} />
	</div>
  );
};
ReactDOM.render(<App />, document.getElementById("root"));

Every time the state is updated, the component re-renders, and the debounce function is reattached to the debouncedApiCall variable. As a result, the logs are printed every time the state changes, and the debounce needs to be fixed.

To avoid this, we can wrap the debounce function inside useCallback, and the program will write the memoized debounce function that will change only if dependencies change.

import React, { useState, useCallback } from "https://esm.sh/react@18.2.0";
import ReactDOM from "https://esm.sh/react-dom@18.2.0";
const App = () => {
  const [text, setText] = useState("");
  const _debounce = (func, delay) => {
	let inDebounce;
	return function () {
  	 const context = this;
  	 const args = arguments;
  	 clearTimeout(inDebounce);
  	 inDebounce = setTimeout(() => func.apply(context, args), delay);
	};
  };
  const makeApiCall = (e) => {
	console.log(e, "Making an API call");
  };
  const debounce = useCallback(_debounce(makeApiCall, 2000), []);
 
  const onChangeHandler = (e) => {
	setText(e.target.value);
	debounce(e.target.value);
  };
  return (
	<div>
  	<input type="text" onChange={onChangeHandler} value={text} />
	</div>
  );
};
ReactDOM.render(<App />, document.getElementById("root"));

We don’t want to re-render the _debounce function, so we are not passing any dependencies. This code will print the log only when the user has stopped writing and 2000 ms have passed. Notice in the code that we are passing the value to debounce(e.target.value) because it cannot pull the value of the state.

Even though it seems to be working fine, it is not. If you have ESLint enabled, you will notice that we are getting the following linting error for the useCallback hook:

error React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead  react-hooks/exhaustive-deps

This is because the dependencies of the function that useCallback is taking as input are not known, so ESLint wants us to write an inline function.

Also, the _debounce function is re-created on every re-render, thus changing its reference and defeating the purpose of caching it.

To fix this, we can create a custom useDebounce hook and use it.

import { useState, useCallback, useRef } from "react";
const useDebounce = (func, delay) => {
  const inDebounce = useRef();
  const debounce = useCallback(
	function () {
  	const context = this;
  	const args = arguments;
  	clearTimeout(inDebounce.current);
  	inDebounce.current = setTimeout(() => func.apply(context, args), delay);
	},
	[func, delay]
  );
  return debounce;
};
const App = () => {
  const [text, setText] = useState("");
  const makeApiCall = useCallback((e) => {
	console.log(e, "Making an API call");
  }, []);
  const debounce = useDebounce(makeApiCall, 2000);
  const onChangeHandler = (e) => {
	setText(e.target.value);
	debounce(e.target.value);
  };
  return (
	<div>
  	<input type="text" onChange={onChangeHandler} value={text} />
	</div>
  );
};

All Syncfusion’s 80+ React UI components are well-documented. Refer to them to get started quickly.

useMemo hook

Syntax

const computedValue = useMemo(()=>{
  doSomething();
}, [dependencies]);

useMemo works similarly to the useCallback, but rather than returning the function, it returns the computed value from the function, i.e. the function’s output, and only recomputes the function when the dependencies change to provide the new result.

For example, let’s say we have a child component that should render only when the text has a specific value.

import React, { useState, useCallback } from "https://esm.sh/react@18.2.0";
import ReactDOM from "https://esm.sh/react-dom@18.2.0";
const ChildComponent = ({ text }) => {
  return <p>{text}</p>;
};
const App = () => {
  const [text, setText] = useState("");
  const onChangeHandler = (e) => {
	setText(e.target.value);
  };
  return (
	<div>
  	<input type="text" onChange={onChangeHandler} value={text} />
  	<ChildComponent text={text} />
	</div>
  );
};
ReactDOM.render(<App />, document.getElementById("root"));

If the conditional check were placed within the child component, it would trigger a re-render each time the parent’s state updates, regardless of our intention to limit it to certain values.

The useMemo hook addresses this. By employing this hook, the child component becomes memoized, ensuring it only re-renders when the conditions specified in its dependencies are satisfied.

In this instance, the child component will exclusively render when the text matches the term “syncfusion.”

import React, { useState, useMemo } from "https://esm.sh/react@18.2.0";
import ReactDOM from "https://esm.sh/react-dom@18.2.0";
const ChildComponent = ({ text }) => {
  return <p>{text}</p>;
};
const App = () => {
  const [text, setText] = useState("");
  const onChangeHandler = (e) => {
	setText(e.target.value);
  };
 
  const memoizedChildComponent = useMemo(() => <ChildComponent text={text} />, [text === 'syncfusion'])
  return (
	<div>
  	<input type="text" onChange={onChangeHandler} value={text} />
  	{memoizedChildComponent}
	</div>
  );
};
ReactDOM.render(<App />, document.getElementById("root"));

Be amazed exploring what kind of application you can develop using Syncfusion React components.

Conclusion

Thanks for reading. This blog delved into the two most potent React hooks, namely useCallback and useMemo, explaining their functions and exploring their optimal application to sidestep redundant computations when React components re-render.

I encourage you to explore these exceptional features and share your experience in the comment section below.

Have you experienced Syncfusion? Feel free to download the product setup here. If you’re new to Syncfusion, enjoy a complimentary 30-day trial.

Should you need assistance, don’t hesitate to reach out via our support forumssupport portal, or feedback portal. We’re dedicated to aiding you at every step!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed