简体   繁体   English

为什么我的 React 应用程序中的 fetch 语句会导致两次调用?

[英]Why is the fetch statement in my react app resulting in two calls?

Can anybody please explain to me why the fetch statement is resulting in 2 API calls?有人可以向我解释为什么 fetch 语句会导致 2 个 API 调用吗? Both the chrome console and dev tools > network tab is showing two versions. chrome 控制台和开发工具 > 网络选项卡都显示两个版本。 The following is the code that I am using.以下是我正在使用的代码。

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

const App = () => {

  const [contacts, setContacts] = useState([]);

  fetch("https://randomuser.me/api/?results=3")
  .then(response => response.json())
  .then(data => console.log(data));

  return (

    <>
      {
        contacts.map(contact => (
          <ContactCard
            avatar="https://via.placeholder.com/150"
            name={contact.name}
            email={contact.email}
            age={contact.age}
          />
        ))
      }
    </>
  )
};


const ContactCard = props => {

  const [showAge, setShowAge] = useState(false);

  return (
    <div className="contact-card">
      <img src="https://via.placeholder.com/150" alt="profile" />
      <div className="user-details">
        <p>Name: {props.name}</p>
        <p>Email: {props.email}</p>

        <button onClick={() => setShowAge(!showAge)}>{!showAge ? 'Show' : 'Hide'} Age</button>

        {
          showAge && <p>Age: {props.age}</p>
        }

      </div>
    </div>
  );
};

export default App;
const App = () => {

  const [contacts, setContacts] = useState([]);
  // the issue is here, each time the component renders this statement will be exectuted
  fetch("https://randomuser.me/api/?results=3")
  .then(response => response.json())
  .then(data => console.log(data));

  // if you want to execute code after component is mounted into dom, use useEffect
  // like this

   useEffect(() => {
        fetch("https://randomuser.me/api/?results=3")
        .then(response => response.json())
        .then(data => console.log(data));
   }, []) // the second param for useEffect is dependencies array, pass an empty array if you want your effect to run only once (which is equivalent to componentDidMount in react class based components)
  return (

    <>
      {
        contacts.map(contact => (
          <ContactCard
            avatar="https://via.placeholder.com/150"
            name={contact.name}
            email={contact.email}
            age={contact.age}
          />
        ))
      }
    </>
  )
};

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

相关问题 React Native Fetch API不返回我的调用 - React Native Fetch API not returning my calls 为什么在我的 React 应用程序中无法获取 function - Why the fetch function does not work from my React app 为什么我的反应应用程序和快速服务器之间的获取请求不起作用? - Why are my fetch requests between my react app and express server not working? 为什么通过提取API请求记录我的React状态setState时会得到两个响应? - Why am I getting two responses when I log my React state, setState by a fetch API request? React:为什么它会跳过我的“获取”function? - React: Why is it skipping over my “fetch” function? 为什么在我的反应应用程序中使用 fetch 发出的调用在邮递员中成功执行时会失败? - Why post call made using fetch in my react app fails when the same is successfully executed in postman? 为什么 fetch 返回我的 React 应用程序的 index.html 而不是我提供的 URL? - Why is fetch returning my React app's index.html instead of the URL I provided? 为什么我的 React-Express 应用程序无法从 express 后端获取和显示数据? - Why my React-Express app can not fetch and display data from express backend? 在 React 世界中获取调用:ReactJS - fetch calls in react world: ReactJS 为什么我的 React 应用程序中的两个点击事件同时运行? - Why do my two click events in my React app run at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM