Home   reactjs  

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:

  1. 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.
  2. 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.
  3. 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.
  4. Memoization Techniques:

    • React provides hooks like React.memo, useMemo, and useCallback 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.
  5. 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.
  6. Component-Level Optimizations:

    • Components can implement shouldComponentUpdate (in class components) or use React.PureComponent to perform shallow comparisons of props and state, preventing unnecessary re-renders.
    • Function components can use React.memo to achieve a similar effect.

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 (
      <button onClick={increment}>Increment</button>
      <ExpensiveComponent value={value} />

export default App;

In this example:

Published on: Jul 05, 2024, 08:28 AM  


Add your comment