Thursday, October 2, 2025
HomeLanguagesReactJS forceUpdate() Method

ReactJS forceUpdate() Method

The components in React will re-render only if the state of the component or the props passed to it changes but if we need to re-render the component if some data changes then we will use forceUpdate() method of React. Calling the forceUpdate() will forcibly re-render the component and thus calls the render() method of the component skipping the shouldComponentUpdate() method.

Tip: Normally, avoid all uses of forceUpdate() and only read from this.props and this.state in render().

Syntax:

component.forceUpdate(callback)

While there are certainly some use cases for using the forceUpdate() method but it’s better to use hooks, props, state, and context to re-render the component whenever you need.

Creating React Application:

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

npx create-react-app functiondemo

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

cd functiondemo

Project Structure: It will look like the following.

Project Structure

Example: In this example, we are going to build a React application that re-render the component when the button is clicked by calling the forceUpdate() method.

App.js: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

Javascript




import React from 'react';
  
class App extends React.Component {
    reRender = () => {
  
        // Calling the forceUpdate() method
        this.forceUpdate();
    };
    render() {
  
        console.log('Component is re-rendered');
        return (
            <div>
                <h2>GeeksForGeeks</h2>
                <button onClick={this.reRender}>
                    Click To Re-Render
                </button>
            </div>
        );
    }
}
export default App;


Note: You can apply your own styling to the application.

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

npm start

Output: 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32330 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11926 POSTS0 COMMENTS
Shaida Kate Naidoo
6817 POSTS0 COMMENTS
Ted Musemwa
7078 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6774 POSTS0 COMMENTS