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 update the State of a component in ReactJS ?
Next article icon

How re-render a component without using setState() method in ReactJS ?

Last Updated : 13 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In React, to re-render a class-based component with an updated state we generally use the setState() method. But instead, we can use other methods to re-render a component without using setState().

Prerequisites: 

  • NPM & Node.js
  • React JS
  • ReactJS props
  • ReactJS forceUpdate() method

Approaches to re-render a component without using setState() method are.

Table of Content

  • By changing props
  • Using the forceUpdate() method

Steps to Create 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

Project Structure:


Screenshot-from-2023-11-10-14-57-33


Method 1: by changing props

If we pass the state of the parent component as a prop to the child and call setState on the parent, it will cause the re-render of the child component as its props are changed.

Example: This code implements re-render by changing the props passed to the component. 

JavaScript
// Filename - App.js  import React, { Component } from "react";  class App extends Component {     constructor(props) {         super(props);          // Set initial state of parent         this.state = { category: "Coder" };          // Binding this keyword         this.updateState = this.updateState.bind(this);     }      updateState() {         // Changing state         this.setState({ category: "Geek" });     }      render() {         return (             <div>                 <Child category={this.state.category} />                 <button onClick={this.updateState}>                     Click me!                 </button>             </div>         );     } }  class Child extends Component {     render() {         return (             <div>                 <p>Hi {this.props.category}!</p>             </div>         );     } }  export default App; 

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

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

re-render of child component by changing props

Method 2: Using the forceUpdate() method

The forceUpdate method forcible re-render the component. When forceUpdate is called it directly calls the render method by skiping other lifecycle methods and update the interface.

Caution: Even though forceUpdate solves our problem but it is advised to use setState or changing props to re-render a component as forceUpdate method skips the proper lifecycle of rendering of a component. See this for detail.

Example: This example implements re-render forcibly using the forceUpdate method. 

JavaScript
// Filename - App.js  import React, { Component } from "react";  class App extends Component {     reRender = () => {         // force update         this.forceUpdate();     };      render() {         return (             <div>                 <h2>Random Number Generator</h2>                 <p>                     Your Number:{" "}                     {Math.floor(Math.random() * 10) + 1}                 </p>                 <button onClick={this.reRender}>                     Generate!                 </button>             </div>         );     } }  export default App; 

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

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

generate a random number by forceUpdate method

Next Article
How to update the State of a component in ReactJS ?

B

biswajitkaushik02
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • React-Questions

Similar Reads

  • 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
  • How to create a Read More component in ReactJS?
    Creating a Read More component in React JS refers to hiding and displaying the text content on the page. It can be achieved by setting the state variable and display the content accordingly. PrerequisitesNode.JS and npmReact JSReact JS useState() hookApproachTo create a Read More component in React
    3 min read
  • How to Send state/props to Another Component in React with onClick?
    The props and state are the main concepts of React. Actually, only changes in props and/ or state trigger React to re-render your components and potentially update the DOM in the browser Props are used to pass data from a parent to a child component.State is used to manage dynamic data within a comp
    3 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 locally manage component's state in ReactJS ?
    Any component in React JS majorly depends on its props and state to manage data. A component's state is private to it and is responsible for governing its behavior throughout its life. A state is nothing but a structure that records any data changes in a react application. It can be used for storing
    2 min read
  • How To Use setInterval() Method Inside React Components?
    The setInterval() method executes a function repeatedly at a specified interval. We can use the setInterval method in a React component to update the component's state or perform other actions. Syntax: setInterval(callback, delay);callback: The function you want to run periodically.delay: The time i
    3 min read
  • How to set Parent State from Children Component in ReactJS?
    To set the parent state from a child component, we use React’s unidirectional data flow. Instead of directly passing data, we pass a function that enables the child to send data back to set the parent state. Prerequisites:React JSuseState HookApproachTo set parent state from child component in React
    2 min read
  • How to optimize React component to render it ?
    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 compo
    3 min read
  • How to prevent a component from rendering ?
    In React JS, preventing a component from rendering simplifies to conditional rendering of the component. When UI is designed using React, we come across a situation when components are to be rendered on the screen based on some condition. For eg, In a University Information System, when a teacher lo
    3 min read
  • 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
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