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
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
, andshouldComponentUpdate
methods- Class component static
getDerivedStateFromProps
method- Function component bodies
- State updater functions (the first argument to
setState
)- Functions passed to
useState
,useMemo
, oruseReducer
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.