Open In App

What is the use of shouldComponenUpdate() methods in ReactJS ?

Last Updated : 24 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The `shouldComponentUpdate()` method in class-based components is invoked before each re-render, excluding the initial render, triggered by updated props or state. Its default value is true but can be customized using conditional JSX, mainly employed for optimizing React web apps by preventing unnecessary component re-renders.

Prerequisite:

Steps to Create the React Application:

Step 1: Create a React application using the following command.

npx create-react-app foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername

Step 3: Run the development server by using the following command:

npm start

Project Structure:

The updated dependencies in package.json file will look like:

"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}

Approach:

  • App.js Component:
    • Contains a `geeksCourses` array with course data and a `cart` state using the `useState()` hook.
    • Applies basic styles and passes down props with respective names.
  • Courses.js Component:
    • Adds basic styles, destructures `geekCourses` and `setCart` properties from props.
    • Implements an onClick handler for each course in the array to update the user’s cart.
  • Cart.js Component:
    • Demonstrates the use of the `shouldComponentUpdate()` method.
    • Compares `nextProps` (title) with the current cart’s title to determine if the component should update.
    • Uses conditional logic to display data when the cart is initially empty and shows the cart otherwise.

Example: Write the following code in the App.js, Courses.js and Cart.js.

JavaScript
import { useState } from "react";
import "./App.css";
import Cart from "./Components/Cart";
import Courses from "./Components/Courses";

function App() {
    let geekCourses = [
        {
            title: "App development with Java and Kotlin",
            price: 10000,
            duration: 5,
            mentor: "Aman Negi",
            TA: "Rahul Negi",
            classesPerWeek: 5,
        },
        {
            title: "Web development Nodejs",
            price: 9000,
            duration: 4,
            mentor: "Riya Rawat",
            TA: "Mihir Rawat",
            classesPerWeek: 4,
        },
        {
            title: "MERN stack ",
            price: 12000,
            duration: 6,
            mentor: "Kartik Sayana",
            TA: "Amogh Sayana",
            classesPerWeek: 6,
        },
        {
            title: "Machine Learning with python",
            price: 10000,
            duration: 6,
            mentor: "Rohit Jain",
            TA: "Utkarsh Jain",
            classesPerWeek: "5",
        },
    ];

    const [cart, setCart] = useState({});

    return (
        <>
            <div style={{ width: "50%", 
                margin: "auto", 
                textAlign: "center" 
            }}>
                <h2>Geeks Courses</h2>
            </div>
            <div style={{ display: "flex", alignItems: "center" }}>
                <Courses geekCourses={geekCourses} 
                    setCart={setCart} cart={cart} />
                <Cart cart={cart} />
            </div>
        </>
    );
}

export default App;
JavaScript JavaScript

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output:



Similar Reads

three90RightbarBannerImg