Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials React React.memo & Lazy Loading
React Intermediate FREE

React.memo & Lazy Loading

Lesson 1 of 17 Intermediate Interactive

React.memo prevents re-renders of unchanged components. lazy() enables code splitting.

Syntax

REACT
const MemoizedComponent = React.memo(MyComponent);
const LazyComponent = React.lazy(() => import('./MyComponent'));
React.memo and Lazy Loading
REACT
import React, { memo, lazy, Suspense } from "react";

const HeavyComponent = memo(function HeavyComponent({ data }) {
    console.log("Rendering HeavyComponent");
    return <div>{data}</div>;
});

const LazyComponent = lazy(() => import("./LazyComponent"));

function App() {
    return (
        <div>
            <HeavyComponent data="Hello" />
            <Suspense fallback={<p>Loading...</p>}>
                <LazyComponent />
            </Suspense>
        </div>
    );
}

Practice

1
Exercise

Practice this concept.

Answer
Write the code as shown above.

Quick Quiz

1

What did you learn?

This covers the basics.

Interview Questions

It is a fundamental React feature.