why react is better even though components are rendered many times
React achieves high performance even though components can render many times due to several key features and optimizations:
-
Virtual DOM (VDOM):
- React uses a Virtual DOM, which is a lightweight representation of the actual DOM. When the state or props of a component change, React creates a new VDOM tree and compares it with the previous one (a process called "reconciliation").
- Only the differences (or "diffs") between the old and new VDOM are identified, and React updates only those parts of the actual DOM that have changed. This minimizes the number of direct DOM manipulations, which are expensive in terms of performance.
-
Efficient Reconciliation:
- React's reconciliation algorithm, also known as the "diffing algorithm," efficiently calculates the minimum number of operations required to update the actual DOM to match the new VDOM.
- It uses key properties to optimize the rendering of lists and can effectively handle adding, removing, or reordering elements.
-
Batching Updates:
- React batches multiple state updates and renders into a single update to reduce the number of re-renders. This reduces the overhead of multiple updates happening in quick succession.
- For example, multiple
setState
calls in event handlers are batched together and trigger only one re-render.
-
Memoization Techniques:
- React provides hooks like
React.memo
,useMemo
, anduseCallback
to memoize components, values, and functions, respectively. This prevents unnecessary re-renders by reusing previous computations or component instances if the inputs haven't changed.
- React provides hooks like
-
Concurrent Mode (Experimental):
- React's Concurrent Mode (still experimental) allows rendering to be interrupted and resumed, making the UI more responsive by prioritizing updates that are crucial for user interaction over less important ones.
-
Component-Level Optimizations:
- Components can implement
shouldComponentUpdate
(in class components) or useReact.PureComponent
to perform shallow comparisons of props and state, preventing unnecessary re-renders. - Function components can use
React.memo
to achieve a similar effect.
- Components can implement
Example: Avoiding Unnecessary Re-renders with React.memo
import React, { useState, useCallback } from 'react';
// ExpensiveComponent only re-renders when its props change
const ExpensiveComponent = React.memo(({ value }) => {
console.log('ExpensiveComponent rendered');
return <div>{value}</div>;
});
const App = () => {
const [count, setCount] = useState(0);
const [value, setValue] = useState('Hello');
// useCallback memoizes the function so it doesn't change on every render
const increment = useCallback(() => setCount(count + 1), [count]);
return (
<div>
<button onClick={increment}>Increment</button>
<ExpensiveComponent value={value} />
</div>
);
};
export default App;
In this example:
ExpensiveComponent
is wrapped withReact.memo
, so it only re-renders if itsvalue
prop changes.- The
increment
function is memoized usinguseCallback
, so it doesn't trigger re-renders ofExpensiveComponent
unnecessarily.
Published on: Jul 05, 2024, 08:28 AM