简体   繁体   English

为什么我的 create-react-app console.log 两次?

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

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.我只是使用 create-react-app 设置制作了一个简单的配方获取应用程序,但是当我尝试记录响应时,它记录了两次。 I went backwards and deleted code until it stopped happening and for whatever reason it starts when I use the state hook:我倒退并删除了代码,直到它停止发生,无论出于何种原因,当我使用 state 挂钩时它开始:

import React, { useState } from 'react';
import './App.css';


function App() {
  const APP_ID = '092fa53f';
  const APP_KEY = '6fcf8c591c129cc3d01aefbda0d8a4d8';
  const recipe_url = `https://api.edamam.com/search?q=chicken&app_id=${APP_ID}&app_key=${APP_KEY}`;

  const [recipes, setRecipes] = useState(0);

  return (
    <div className="App">
      {console.log('test')}
    </div>
  );
}

export default App;

This is on purpose, it's part of React.StrictMode (specifically to detect unexpected side effects ):这是故意的,它是React.StrictMode的一部分(专门用于检测意外的副作用):

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 组件constructorrendershouldComponentUpdate方法
  • Class component static getDerivedStateFromProps method Class 组件 static getDerivedStateFromProps方法
  • Function component bodies Function 组件体
  • State updater functions (the first argument to setState ) State 更新函数( setState的第一个参数)
  • Functions passed to useState , useMemo , or useReducer传递给useStateuseMemouseReducer的函数

If you remove the StrictMode element from index.js , you'll see the output only gets logged once:如果您从index.js中删除StrictMode元素,您将看到 output 只记录一次:

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

Note that in strict mode this only happens in development , not in production.请注意,在严格模式下,这只发生在开发中,而不是生产中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么console.log in react 运行两次 - Why console.log in react runs twice 如何在使用 create-react-app 创建的 React 应用程序的生产版本中删除 console.log? - How can remove console.log in the production build of a React application created using create-react-app? 如何在使用“create-react-app”创建的 reactjs 的生产过程中删除 console.log - how to remove console.log during production in reactjs created with `create-react-app` 为什么 create-react-app 初始化两次? - Why does create-react-app initialize twice? 为什么console.log在react js中记录两次? - Why is console.log logging twice in react js? 为什么 `Promise.then` 在 React 组件中被调用两次,而不是在 console.log 中? - Why is `Promise.then` called twice in a React component but not the console.log? 为什么我的create-react-app无法在移动浏览器上显示? - Why does my create-react-app not showing on mobile browsers? 为什么我的 create-react-app 没有服务人员? - Why does my create-react-app have no service workers? console.log(this.props.store) 在我创建的 React 应用程序中返回未定义? - console.log(this.props.store) returning undefined in my create react app? 为什么这个 onClick 事件处理程序在我的 create-react-app 中触发两次 - Why is this onClick event handler firing twice in my create-react-app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM