Why does my create-react-app console.log twice?

后端 未结 1 1390
谎友^
谎友^ 2021-01-23 09:02

I\'m just making a simple recipe fetching app using the create-react-app setup, but when I tried logging the response it logged it twice. I went backwards and deleted code until

相关标签:
1条回答
  • 2021-01-23 09:27

    This is on purpose, it's part of React.StrictMode (specifically to detect unexpected side effects):

    Strict mode can’t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:

    • Class component constructor, render, and shouldComponentUpdate methods
    • Class component static getDerivedStateFromProps method
    • Function component bodies
    • State updater functions (the first argument to setState)
    • Functions passed to useState, useMemo, or useReducer

    If you remove the StrictMode element from index.js, you'll see the output only gets logged once:

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

    Note that in strict mode this only happens in development, not in production.

    0 讨论(0)
提交回复
热议问题