Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to use HOCs to reuse Component Logic in React ?
Next article icon

How to optimize React component to render it ?

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

ReactJS mainly depends upon the props (which are passed to it) and the state of the Component. Hence to reduce the number of times Component renders we can reduce the props and state it depends upon. This can be easily done by separating the logic of one component into several individual child components.

Steps to Create the React App:

Step 1: Create a fresh React Native Project by running the command

npx create-react-app demo

Step 2: Now go into your project folder i.e. language demo.

cd demo

Project Structure:

Project Structure

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: We have now three components named Parent.js, Child1.js, and Child2.js.We have declared a counter state in all three of them and also a Button to increase the value of count.

  1. When the app loads for the first time all three of the components get rendered for the first time.
  2. Now when we click in the Parent’s Button the counter state of the parent component increases by 1, this results in the rendering of the Parent component as since Child1 and Child2 are rendered in the same component they both also get rendered.
  3. Now we click the Button in Child1 which changes the counter state of the Child1 Component, but this time only the Child1 component gets rendered. That’s because both the Parent and Child2 components are separate from Child1 rendering logic.
  4. Now we click the Button in Child2 which changes the counter state of the Child2 Component, only the Child2 component gets rendered. That’s because both the Parent and Child1 components are separate from Child2 rendering logic.

Example: Below is the practical implementation of the above code and write the below code in the following files:

  • App.js: App component calling parent component
  • Parent.js: Parent component calling both the child component and console parent is entered.
  • Child1.js: we click the Button in Child1 which changes the counter state of the Child1 Component, but this time only the Child1 component gets rendered.
  • Child2.js: we click the Button in Child2 which changes the counter state of the Child2 Component, only the Child2 component gets rendered.
App.js
import logo from "./logo.svg"; import "./App.css"; import React from "react"; import Parent from './screens/parent';  function App() {      return (         <div className="App">             <Parent />         </div>     ); }  export default App; 
JavaScript
//Parent.js import React, { Component } from "react"; import Child1 from "./Child1"; import Child2 from "./Child2"; export class parent extends Component {     constructor(props) {         super(props);         this.state = {             countInParent: 0,         };     }     render() {         console.log("Parent is rendered");         return (             <div>                 <h1>Count In Parent {this.state.countInParent}</h1>                 <button                     type="button"                     className="btn btn-primary btn-lg"                     onClick={() => this.setState({                         countInParent: this.state.countInParent + 1                     })}                 >                     button in Parent                 </button>                 <Child1 />                 <Child2 />             </div>         );     } }  export default parent; 
JavaScript
//Child1.js import React, { Component } from "react";  export class child1 extends Component {     constructor(props) {         super(props);         this.state = {             countInChildOne: 0,         };     }     render() {         console.log("Child 1 is rendered");         return (             <div>                 <h1>Count In Child One {this.state.countInChildOne}</h1>                 <button                     type="button"                     className="btn btn-primary btn-lg"                     onClick={() =>                         this.setState({                             countInChildOne: this.state.countInChildOne + 1                         })                     }                 >                     Button in Child One                 </button>             </div>         );     } }  export default child1; 
JavaScript
//Child2.js import React, { Component } from "react";  export class child2 extends Component {     constructor(props) {         super(props);         this.state = {             countInChildTwo: 0,         };     }     render() {         console.log("Child 2 is rendered");         return (             <div>                 <h1>Count In Child Two {this.state.countInChildTwo}</h1>                 <button                     type="button"                     className="btn btn-primary btn-lg"                     onClick={() =>                         this.setState({                              countInChildTwo: this.state.countInChildTwo + 1                          })                     }                 >                     Button In Child Two                 </button>             </div>         );     } }  export default child2; 

Step to run the app:

npm start

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20240722162132/ezgifcom-gif-to-mp4-converter-(7).mp4




Next Article
How to use HOCs to reuse Component Logic in React ?

R

rahulschauhan50
Improve
Article Tags :
  • JavaScript
  • ReactJS
  • Web Technologies
  • React-Questions

Similar Reads

  • Methods to Optimize the re-render in React-Redux Connected Component?
    For a smooth user experience, fast rendering in React applications is crucial. Using React-Redux can sometimes lead to performance issues due to unnecessary re-renders of connected components. This article will explore several methods to avoid the rendering of these components. By implementing these
    4 min read
  • How to use HOCs to reuse Component Logic in React ?
    In React, making reusable components keeps your code neat. Higher-order components (HOCs) are a smart way to bundle and reuse component logic. HOCs are like magic functions that take a component and give you back an upgraded version with extra powers or information. HOCs can be implemented in a few
    4 min read
  • How to make Reusable React Components ?
    ReactJS is a JavaScript library used to build single-page applications (SPAs). React makes it easy to create an interactive user interface by using a component-based approach. In this article, we will learn about Reusable React Components with examples. Table of Content What are Reusable React Compo
    4 min read
  • How do you optimize the rendering of connected components in React-Redux?
    React-Redux is a powerful combination for building complex web applications, particularly those with dynamic user interfaces. However, as applications grow in size and complexity, rendering performance can become a concern. Optimizing the rendering of connected components in React-Redux is crucial f
    3 min read
  • How to conditionally render components in ReactJS ?
    React is a declarative, efficient, and flexible JavaScript library for building user interfaces. It’s ‘V’ in MVC. ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. It is maintained by Facebook. PrerequisitesNodeJS or NPMReact JSReact
    3 min read
  • How to combine multiple reducers in ReactJS ?
    To combine multiple reducers in React JS we have a function called combineReducers in the redux. This basically helps to combine multiple reducers into a single unit and use them. Approach In React, to combine and implement multiple reducers combineReducers methods are used. It manages multiple redu
    4 min read
  • How to put ReactJS component inside HTML string ?
    Putting the React JS component inside the HTML library is not recommended and may lead to conflict in React lifecycle methods. But it can be done by parsing the HTML String into DOM using the html-to-react module. html-to-react is a lightweight library that is responsible for converting raw HTML to
    3 min read
  • How useMemo Hook optimizes performance in React Component ?
    The useMemo hook in React optimizes performance by memoizing the result of a function and caching it. This caching ensures that expensive computations inside the useMemo callback are only re-executed when the dependencies change, preventing unnecessary recalculations and improving the rendering perf
    2 min read
  • Re-rendering Components in ReactJS
    Re-rendering is an important concept in ReactJS as it determines how and when components update. Understanding the re-rendering process is essential for optimizing the performance of React applications. What is Re-rendering in ReactJS?In React, a re-render happens when a component's state or props c
    5 min read
  • How to update the State of a component in ReactJS ?
    To display the latest or updated information, and content on the UI after any User interaction or data fetch from the API, we have to update the state of the component. This change in the state makes the UI re-render with the updated content. Prerequisites: NPM & Node.jsReact JSReact StateReact
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences