4

I have created a react project and added constructor and render menthod to it, while running it I was expecting both constructor and render run once only, but both are running twice. First constructor is running twice and then render. Can someone please help, same was happening to other life cycle methods with me.


import React, {Component} from 'react';
class App extends Component {
  constructor(props) {
    super(props)
    console.log('Constructor')
  }

  render() {
    console.log('render')
    return (
      <h1>My Favorite Color is </h1>
    );
  }
}
export default App;

Here is my index.js.. for how it is called


import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

<code>This is my output</code>

6
  • 1
    How is the component being called? Commented Mar 23, 2020 at 17:30
  • Probably you are rendering App component twice. Commented Mar 23, 2020 at 17:31
  • It is default way, I have done these changes only after creating a project using npx create-react-app.... Commented Mar 23, 2020 at 17:31
  • 1
    Can you post above code in question. Commented Mar 23, 2020 at 17:32
  • It is normal and so designed. Does this answer your question? stackoverflow.com/questions/48846289/… Commented Mar 23, 2020 at 17:34

3 Answers 3

8

It works for me, if I replace below code

ReactDOM.render(
    <React.StrictMode>
      <App />
    </React.StrictMode>,
    document.getElementById('root')
  )

by below line (but the above code is the one I got as default code on creating a project using create-react-app command.

ReactDOM.render(<App />, document.getElementById('root'));
Sign up to request clarification or add additional context in comments.

1 Comment

I also got the same problem and it works as your solution. upvote.
4

I had the same problem and just removed React.StrictMode and now everything renders only once.

Comments

2

Actually this is the intended behavior of StrictMode as documented in the React Docs. It is explained in the docs that React has two phases, namely render and commit. In the future, Concurrent Mode will try to avoid blocking the browser thread by stopping and replaying some lifecycle functions like render, constructor, and more. Strict mode has this feature and it replays these lifecycle methods.

React Docs also say that these lifecycle functions should NOT contain any side-effects. Having side effects will cause memory leaks and invalid application state.

My personal opinion is that you should keep all the side-effects to safe lifecycle functions as described in the docs. Link to the specific section of the docs that explain this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.