0

Firstly sorry if my English is not perfect, i learn :x

I have a problem withe react, all my components is run two time, for example if i write "console.log('test');" in the render function, so i see "test" two time in the console...

just for specify, i don't have this problem when i use a function for a component and in class all default methods (constructor, render, componentDidMount, ... ) is run two time when the components is create or modify (not destruct the componentWillUnmount is been run one time).

the problem is not from my code, i tried to create a new app (with create-react-app) and change function by a class but the result it's the same (even on a other computer)

the only file i have change after create the project (i have change only line 1, line 5 and line 7):

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    console.log('test');
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
        </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
        </a>
        </header>
      </div>
    );
  }

}

export default App;

I specify too, i have already read that react prefer function now, class is depreciated, but i learn currently react and i like explore all possibility :)

Thanks for your help ;) and sorry again for my English :x

Edite :

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  constructor() {
    super()
    console.log("test")
  }
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
        </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
        </a>
        </header>
      </div>
    );
  }

}

export default App;

In this case console.log() is run two time too, but the constructor is not run when we do an update so it's not that the problem

  • Does this answer your question? [Why is my React component is rendering twice?](https://stackoverflow.com/questions/48846289/why-is-my-react-component-is-rendering-twice) – Thomas Maddocks Apr 12 '20 at 23:38

1 Answers1

1

First of all, you should understand the usage of React lifecycle methods such as render(), componentDidMount().

The render() method is called whenever state changes, when you run setState() method render() method will be triggered again. So render() is the method which is customizing your UI continually according to the application state. You can't control the method to run only once since you need to re-generate the UI frequently. You have to refer to the stateful and stateless components in React.

Stateless components will be generated once and cannot change the state unless you re-create the component.

Life cycle Source

The reason for the stateful component render() method is triggered two times. You have to understand React Virtual DOM which React initially renders the changes in virtual DOM and identify the specific changes effected then it renders only the changed UI in a real DOM.

Updating the browser’s DOM is a three-step process in React.

  1. Whenever anything may have changed, the entire UI will be re-rendered in a Virtual DOM representation.

  2. The difference between the previous Virtual DOM representation and the new one will be calculated.

  3. The real DOM will be updated with what has actually changed. This is very much like applying a patch.

Literally the render method will be called in virtual DOM and real DOM in stateful components. Write only the UI logics in the render() method do not write functional logics such as sending HTTP requests, you can write those into componentDidMount() method.

Refer the explanation Stateful vs Stateless

Googlian
  • 6,077
  • 3
  • 38
  • 44
  • Yes i have already see this picture and i have understand the usage of react lifecycle i think but i mean i never update state or props so i don't understand :/ And i f i add a constructor withe a console.loge("testConstructor"), i see "testConstructor" two time in the console.. So even if i change state or other the constructor should not launch (i don't call two time my component) So yes if i do a changer in state or i add a new props render is again call but is not the case and even if it was, the constructor should not launch :/ –  Apr 13 '20 at 11:16
  • @Nicolas Please refer the edited question for Virtual DOM – Googlian Apr 14 '20 at 05:06
  • Yes i think i get it (i go read more for be sur) but i don't think the problem it's from that, i edited my ask, look i writed console.log() in the constructor and i see the result two time tooin the console... but the constructor is not run while an update. And thanks for your time ^^' –  Apr 14 '20 at 12:10
  • It won't run the constructor, it will only run render method – Googlian Apr 14 '20 at 14:00
  • Use componentDidMount for your logics – Googlian Apr 14 '20 at 14:01
  • however I assure you that if I did a search in all the project and the only console.log () which exists is in the constructor () except that it is executed well twice... and i'm agree for us componentDidMount, but i want know why all methode is run two time ^^' –  Apr 14 '20 at 16:22
  • I hope my answer would give clear explanation for why it execute twice – Googlian Apr 14 '20 at 17:27
  • oooh ok thanks i understand, it's not all function run two time sorry ^^' just 2 ask if you agree : 1. getDervedStateFromProps, ShouldComponentUpdate and render is run two time i understand why now, but why the contsructor is run two time ? and 2. in this video render and constructor is run one time, you know why ? https://www.youtube.com/watch?v=c5LZZuLAM2w&list=PLmYBIzXGbEzLhBgbGa7ucqGJr5RgmzjWr&index=19#t=13m40s –  Apr 14 '20 at 19:47
  • I think it is because of an update after the video, but like her at less than a year I prefer to ask to be on –  Apr 14 '20 at 19:49