2

When i write console.log('constructor') in class's constructor, the output duplicates the rendering. code rendering

1
  • 1
    Check your index file and remove StrickMode Commented Jun 4, 2020 at 10:55

3 Answers 3

11

You can disable doubled comments with React Developer Tools browser extension

React Developer Tools screenshot

Sign up to request clarification or add additional context in comments.

Comments

9

To solve the problem you have to disable the strict react mode in index.js

1 Comment

This issue will help you better understand why this is happening!
6

If you're on development mode, try disabling "strict mode". On dev mode Reach renders components twice to make dev-only checks and tests to warn you of errors. https://github.com/reactwg/react-18/discussions/96#discussion-3562310

If you're not on dev mode:

This has nothing to do with the console.log duplicating the rendering. It's impossible that it may cause that, because console logging something doesn't affect a component's state, and it won't trigger a new render.

It's normal that components render more than once in a lot of cases

  • Do you have any code updating state in your component? Like inside componentDidMount or anywhere else?
  • Is a parent of this component updating state anywhere? If so, it will cause itself to re-render, which includes its children re-rendering too.

In short, this is not being caused by your console log. If you remove it you won't "see" the double render because it happens fast and you don't have a point of reference (like a log in your console), but it still happens.

2 Comments

This is a new App of create-react-app and i inserted into the constructor console.log('constructor'). But i don't know why the output is duplicated
I recommend you read this it goes over some causes and explanations. If I answered your question, please mark it as accepted 🙏

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.