Languages
[Edit]
EN

React - class component force re-render

0 points
Created by:
Olivier-Myers
514

In this article, we would like to show you how to force re-render in a class component in React.

Any change of state causes component rendering, so using e.g. counter in the state, we are able to force re-rendering always when setState() method changes (increments) the value of our counter.

Runnable example:

// ONLINE-RUNNER:browser;

// Note: Uncomment import lines during working with JSX Compiler.
// import React from 'react';
// import ReactDOM from 'react-dom';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { counter: 0 };
  }

  render() {
    console.log('App component rednering...');
    return (
      <div>
        <button onClick={() => this.setState({ counter: this.state.counter + 1 })}>
         force re-rendering
        </button>
      </div>
    );
  }
}

const root = document.querySelector('#root');
ReactDOM.render(<App />, root);

Note:

Go to this article to see functional component example.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - class component force re-render
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join