How to improve slow React application rendering ?
Last Updated :
25 Jul, 2024
Slow React application rendering as its name suggests is a small or large delay in rendering elements. There may be multiple reasons for that. React uses the concept of Virtual DOM. All the elements in React are rendered using the render method in which we have to provide an id of an element (mostly div) to be rendered. Every element in React is immutable. Hence, to Update the element, we have to call the Render method again.
Prerequisites:
Approach to Improve Slow React Application Rendering:
Slow React Application Rendering may occur due to heavy components, images, bad code practices unnecessary renders due to changes in states. This problem can be reduced by code splitting, avoiding unnecessary re-renders and memoization. We will be using useMemo hook to reduce calculations and updates to improve rendering.
Steps to create React Application
Step 1: Create a React application using the following command:
npx create-react-app useMemo
Step 2: After creating your project folder i.e. useMemo, move to it using the following command:
cd useMemo
Project Structure:

Example 1: This example uses useState hook to store and update state and render on UI.
JavaScript
// Filename - App.js
import { useState } from "react";
import "./App.css";
export default function App() {
// 1st counter state
const [counter1, setCounter1] = useState(0);
// 2nd counter state
const [counter2, setCounter2] = useState(0);
// Sample Heavy Calculation Function
const heavyCalculation = () => {
let i = 0;
for (let outer = 0; outer < 10000; outer++) {
for (let temp = 0; temp < 10000; temp++) {
while (i < 10000) i++;
}
}
return counter1 % 2 === 0 ? true : false;
};
return (
<div className="App">
{heavyCalculation()
? `Counter is even with value ${counter1}`
: `Counter is odd with value ${counter1}`}
<br />
<button
onClick={() => {
setCounter1(counter1 + 1);
}}
>
Increment counter1
</button>
<br />
Counter 2 is {counter2}
<br />
<button
onClick={() => {
setCounter2(counter2 + 1);
}}
>
Increment counter2
</button>
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Here, in the output, a heavy function is being called on Rendering. Now even though it is being used for the first counter, but it still causes render due to counter2 slow.
Example 2: This example implements useMemo Hook to avoid unnecessary update and hence reduce rerenders on the UI.
App.js
import { useState, useMemo } from "react";
import "./App.css";
export default function App() {
// 1st counter state
const [counter1, setCounter1] = useState(0);
// 2nd counter state
const [counter2, setCounter2] = useState(0);
// Our custom useMemo Function
const useMemoFunction = useMemo(() => {
let i = 0;
for (let outer = 0; outer < 10000; outer++) {
for (let temp = 0; temp < 10000; temp++) {
while (i < 10000) i++;
}
}
return counter1 % 2 === 0 ? true : false;
}, [counter1]);
return (
<div className="App">
{useMemoFunction
? `Counter is even with value ${counter1}`
: `Counter is odd with value ${counter1}`}
<br />
<button
onClick={() => {
setCounter1(counter1 + 1);
}}
>
Increment counter1
</button>
<br />
Counter 2 is {counter2}
<br />
<button
onClick={() => {
setCounter2(counter2 + 1);
}}
>
Increment counter2
</button>
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Here, heavy functionality is used by useMemo hook containing counter1 as a dependency. Due to this Counter1 still shows delay, but it does not affect the performance of counter2.
Similar Reads
How to Optimize the Performance of React-Redux Applications?
Optimizing the performance of React-Redux applications involves several strategies to improve loading speed, reduce unnecessary re-renders, and enhance user experience. In this article, we implement some optimization techniques, that can significantly improve the performance of applications. We will
9 min read
How to Implement Caching in React Redux applications ?
Caching is the practice of storing frequently used or calculated data temporarily in memory or disk. Its main purpose is to speed up access and retrieval by minimizing the need to repeatedly fetch or compute the same information. By doing so, caching helps reduce latency, conserve resources, and ult
3 min read
Movie Web Application with ReactJS
React Movie APP is a movie web application project that involves creating components to display a list of movies and details about each movie along with a search functionality. Preview Image of Final Output: Approach for React Movie AppTo create the movie web application in React we will be using an
7 min read
How to call function inside render in ReactJS ?
In React JS, the render method is a fundamental part of a component's lifecycle. It is responsible for rendering the component's JSX markup onto the DOM. While the render method itself should primarily handle rendering JSX, there are scenarios where you may need to call a function inside the render
3 min read
How to implement pagination in React Redux Applications?
Pagination is a common requirement in web applications. It is very helpful when we wish to display or manage a large dataset on the website in an aesthetic manner. Whenever it comes to displaying a large number of data, state handling comes into the picture. The idea of pagination has a good binding
5 min read
How to optimize React component to render it ?
ReactJS mainly depends upon the props (which are passed to it) and the state of the Component. Hence to reduce the number of times Component renders we can reduce the props and state it depends upon. This can be easily done by separating the logic of one component into several individual child compo
3 min read
How To Implement Code Splitting in Redux Applications?
Code splitting in Redux applications refers to the practice of breaking down the application into smaller, manageable chunks that can be loaded on demand. This helps in reducing the initial load time of the application by only loading the necessary parts of the application when needed. Dynamic impor
3 min read
How to Solve too many re-renders Error in ReactJS?
"Too many re-renderers" is a React error that happens after you have reached an infinite render loop, typically caused by code that, in a useEffect hook or the main body of the component itself, unconditionally calls state setters. PrerequisitesNPM & NodeJSReactJSReactJS lifecycleReactJS HooksUn
5 min read
Explain the purpose of render() in ReactJS
Render in React JS is a fundamental part of class components. It is used to display the component on the UI returned as HTML or JSX components. The ReactDOM.render() function takes two arguments, HTML code and an HTML element. Purpose of render()React renders HTML to the web page by using a function
2 min read
How to prevent a component from rendering ?
In React JS, preventing a component from rendering simplifies to conditional rendering of the component. When UI is designed using React, we come across a situation when components are to be rendered on the screen based on some condition. For eg, In a University Information System, when a teacher lo
3 min read