How to Improve React App Performance with Simple Hacks πŸš€

How to Improve React App Performance with Simple Hacks πŸš€

πŸš€ Why React Performance Matters

A slow React app can frustrate users, increase bounce rates, and even impact SEO. Instead of a complete rewrite, you can use simple optimizations to significantly boost your React app’s speed and efficiency.

Let’s explore proven techniques to improve performance while keeping the code clean and scalable.

β€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” β€”

1️⃣ Prevent Unnecessary Re-Renders with **React.memo()** 🧠

By default, React re-renders child components even if their props haven’t changed. This can be inefficient.

βœ… Solution: Use React.memo() to prevent unnecessary re-renders by memoizing components.

❌ Unoptimized Version

const ChildComponent = ({ name }) => {
console.log("Child Rendered!");
return Hello, {name}!;
};

const ParentComponent = () => {
const [count, setCount] = React.useState(0);
return (

setCount(count + 1)}>Increase


);
};

πŸ”Ή Issue: Even though name="John" never changes, ChildComponent re-renders every time the button is clicked.

βœ… Optimized with **React.memo()**

const ChildComponent = React.memo(({ name }) => {
console.log("Child Rendered!");
return Hello, {name}!;
});

πŸ”₯ Now, **ChildComponent** only re-renders if **name** changes!

β€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” β€”

2️⃣ Optimize Lists with **key** and React Virtualization πŸ”₯

Large lists can slow down your app due to excessive re-renders.

βœ… Solution:

  • Use a unique **key** (not index) for lists.
  • Use react-window for rendering large lists efficiently.

❌ Unoptimized List

{items.map((item, index) => (

{item.name}
))}

⚠️ Issue: Using **index** as **key** can cause unnecessary re-renders.

βœ… Optimized with Unique **key**

{items.map((item) => (

{item.name}
))}

πŸ”Ή Why? React efficiently tracks list updates based on unique **id**.

β€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” β€”

πŸš€ Supercharged: Virtualized List for Large Data

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

const MyList = ({ items }) => (

{({ index, style }) =>

{items[index].name}}

);

πŸ”₯ Now, React only renders visible items, boosting performance!

β€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” β€”

3️⃣ Optimize Functions with **useCallback()** and **useMemo()** πŸ”„

❌ Problem: Unnecessary Function Recreation

Each re-render creates new function instances, causing unwanted re-renders in child components.

βœ… Solution:

Use **useCallback()** for Function Memoization

const ParentComponent = () => {
const [count, setCount] = React.useState(0);

const handleClick = React.useCallback(() => {
console.log("Button Clicked!");
}, []);

return ;
};

πŸ”Ή Now, **handleClick** doesn’t change on every render! πŸš€

βœ… Optimize Expensive Calculations with **useMemo()**

If a component performs heavy calculations, it slows down the app. Use useMemo() to cache results.

const expensiveCalculation = (num) => {
console.log("Calculating...");
return num * 2;
};

const MyComponent = ({ num }) => {
const result = React.useMemo(() => expensiveCalculation(num), [num]);
return

{result}
;
};

πŸ”₯ Now, the function only runs when **num** changes!

β€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” β€”

4️⃣ Optimize Images and Assets πŸ“Έ

Large images slow down page load time. Use:
βœ… Lazy Loading (Load images only when needed)
βœ… Next.js Image Optimization (For Next.js projects)

πŸ”Ή React Lazy Loading Example

const LazyImage = ({ src, alt }) => (

);

πŸ”₯ Now, images only load when they appear on the screen!

β€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” β€”

5️⃣ Split Code with React.lazy() & Suspense πŸš€

Instead of loading everything at once, load only what the user needs using code-splitting.

βœ… Solution: Use React.lazy() to dynamically load components.

❌ Without Code-Splitting (Slow)

import HeavyComponent from "./HeavyComponent";

⚠️ Issue: Loads even if not needed!

βœ… With Code-Splitting (Faster)

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

const App = () => (
Loading...}>


);

πŸ”₯ Now, **HeavyComponent** loads only when needed!

β€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” β€”

πŸš€ Final Thoughts

πŸ”Ή Summary of Performance Hacks

Optimization: β€”β€Šβ€”β€Šβ€”β€Šβ€” Benefit:

React.memo()β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” Prevents unnecessary re-renders
Keys & Virtualizationβ€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” Optimizes list rendering
useCallback() & useMemo() β€”β€Šβ€”β€Šβ€”β€Šβ€” Prevents unnecessary function recreations
Code-Splitting with React.lazy()β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” Reduces initial load time
Lazy Loading Imagesβ€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€” Improves loading speed

βœ… Which of these techniques have you used before?
πŸ’¬ Let me know in the comments! 😊

Β