REACT Performance Tips

Optimizing React applications improves speed, responsiveness, and overall user experience. This guide covers various performance optimization techniques, including lazy loading, memoization, avoiding unnecessary re-renders, optimizing API calls, and more.

 

1. Use React.memo to Prevent Unnecessary Re-renders

React re-renders components whenever their parent component updates, even if the props have not changed. Wrapping components with React.memo prevents unnecessary re-renders.

Example: Using React.memo

import React from "react";

const MemoizedComponent = React.memo(function MyComponent({ count }) {
  console.log("Component re-rendered");
  return <p>Count: {count}</p>;
});

export default MemoizedComponent;

Try It Now

βœ” Prevents re-renders if props remain unchanged
βœ” Useful for functional components receiving props

2. Use useCallback to Optimize Function References

In React, inline functions create a new reference on each render, causing unnecessary re-renders. Use useCallback to memoize functions.

Example: Using useCallback

import React, { useState, useCallback } from "react";
import MemoizedComponent from "./MemoizedComponent";

function App() {
  const [count, setCount] = useState(0);

  const handleClick = useCallback(() => {
    setCount((prev) => prev + 1);
  }, []);

  return (
    <div>
      <MemoizedComponent count={count} />
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}

export default App;

Try It Now

βœ” Prevents unnecessary re-renders
βœ” Ensures functions have stable references

3. Use useMemo to Optimize Expensive Computations

Expensive calculations re-run on every render, slowing down performance. Use useMemo to cache the result and recompute it only when dependencies change.

Example: Using useMemo

import React, { useState, useMemo } from "react";

function ExpensiveComponent({ num }) {
  const squaredNumber = useMemo(() => {
    console.log("Computing square...");
    return num * num;
  }, [num]);

  return <p>Squared Number: {squaredNumber}</p>;
}

export default ExpensiveComponent;

Try It Now

βœ” Improves performance by caching expensive computations
βœ” Runs only when dependencies change

4. Lazy Load Components with React.lazy

Instead of loading everything at once, load components only when needed using React.lazy and Suspense.

Example: Lazy Loading Components

import React, { Suspense } from "react";

const HeavyComponent = React.lazy(() => import("./HeavyComponent"));

function App() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <HeavyComponent />
    </Suspense>
  );
}

export default App;

Try It Now

βœ” Reduces initial load time
βœ” Loads components only when required

5. Optimize React Rendering with Virtualized Lists

Rendering large lists slows down performance. Use react-window or react-virtualized for optimized rendering.

Example: Virtualized List Using react-window

npm install react-window

Try It Now

import { FixedSizeList as List } from "react-window";

const Row = ({ index, style }) => <div style={style}>Item {index}</div>;

function App() {
  return (
    <List height={300} itemCount={1000} itemSize={35} width={300}>
      {Row}
    </List>
  );
}

Try It Now

βœ” Renders only visible items
βœ” Improves performance for large lists

6. Optimize API Calls with Debouncing and Throttling

Frequent API calls slow down performance. Use debouncing and throttling to limit API requests.

Example: Debouncing API Calls

import { useState } from "react";

function useDebounce(value, delay) {
  const [debouncedValue, setDebouncedValue] = useState(value);

  useEffect(() => {
    const handler = setTimeout(() => setDebouncedValue(value), delay);
    return () => clearTimeout(handler);
  }, [value, delay]);

  return debouncedValue;
}

function SearchComponent() {
  const [query, setQuery] = useState("");
  const debouncedQuery = useDebounce(query, 500);

  useEffect(() => {
    if (debouncedQuery) {
      console.log("Fetching data for:", debouncedQuery);
    }
  }, [debouncedQuery]);

  return <input onChange={(e) => setQuery(e.target.value)} />;
}

Try It Now

βœ” Reduces unnecessary API calls
βœ” Improves performance of search inputs

7. Optimize Image Loading with Lazy Loading

Large images increase page load time. Use lazy loading to load images only when needed.

Example: Lazy Loading Images

function ImageComponent() {
  return <img src="large-image.jpg" loading="lazy" alt="Optimized Image" />;
}

Try It Now

βœ” Reduces initial page load time
βœ” Loads images only when they appear in the viewport

8. Avoid Inline Functions and Objects in JSX

Inline functions and objects cause unnecessary re-renders because they create a new reference on every render.

❌ Bad Example: Causes Re-renders
<button onClick={() => console.log("Clicked")}>Click Me</button>

Try It Now

βœ… Good Example: Optimized with useCallback
const handleClick = useCallback(() => console.log("Clicked"), []);
<button onClick={handleClick}>Click Me</button>;

Try It Now

βœ” Prevents unnecessary function recreation

9. Use Key Prop Efficiently in Lists

React uses the key prop to track list items. Using an inefficient key (like index) can cause unnecessary re-renders.

❌ Bad Example: Using Index as Key
{items.map((item, index) => (
  <div key={index}>{item.name}</div>
))}

Try It Now

βœ…Β Good Example: Using Unique IDs
{items.map((item) => (
  <div key={item.id}>{item.name}</div>
))}

Try It Now

βœ” Prevents incorrect UI updates
βœ” Improves React’s reconciliation process

10. Minimize Component Re-renders with Context API Optimization

Using React Context without optimization can cause unnecessary re-renders.

Solution: Use Separate Context Providers

Instead of one large context, split it into multiple smaller ones.

 

const ThemeContext = React.createContext();
const AuthContext = React.createContext();

function ThemeProvider({ children }) {
  const [theme, setTheme] = useState("light");
  return (
    <ThemeContext.Provider value={{ theme, setTheme }}>
      {children}
    </ThemeContext.Provider>
  );
}

Try It Now

βœ” Avoids unnecessary re-renders
βœ” Improves performance when context values update

Summary – React Performance Best Practices

  1. Use React.memo to prevent unnecessary re-renders
  2. Use useCallback to memoize function references
  3. Use useMemo to cache expensive computations
  4. Lazy load components with React.lazy
  5. Use virtualized lists for large data sets
  6. Debounce API calls to reduce network requests
  7. Lazy load images to optimize page speed
  8. Avoid inline functions/objects inside JSX
  9. Use efficient keys in lists to improve reconciliation
  10. Optimize Context API usage to prevent unnecessary renders