如何防止子组件在有人访问页面时首次调用API两次

huangapple go评论88阅读模式
英文:

How to stop child component from making api call twice for the very first time when someone visit the page

问题

以下是您要翻译的内容:

所以假设我有一个带有3个选项卡的主页,"All",'Sci-fi',"Comedy"。我在主页中调用子组件并呈现相应的数据。发生的情况是,当用户首次访问时,选项卡被选为"All"。我的子组件会发出相同的API调用两次。如何防止这种情况发生。

import React from 'react';
import ChildComponent from './ChildComponent';

function Home() {
  return (
    <div>
      <h1>Home Component</h1>
      <ChildComponent />
    </div>
  );
}

export default Home;
import React, { useState, useEffect } from 'react';

function ChildComponent() {
  const [data, setData] = useState([]);

  useEffect(() => {
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(data => setData(data))
      .catch(error => console.error('Error fetching data:', error));
  }, []);

  return (
    <div>
      <h2>Child Component</h2>
      <ul>
        {data.map(item => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
    </div>
  );
}

export default ChildComponent;

请注意,上述内容中的代码部分没有翻译。

英文:

So let say I have one home page with 3 tabs, "All", 'Sci-fi',"Comedy".I am calling the child component in the home page and rendering the respected data. What is happening is that when user visit for the first time tab is selected as "All". My child component is making same api call twice.How to prevent it.

import React from &#39;react&#39;;
import ChildComponent from &#39;./ChildComponent&#39;;

function Home() {
  return (
    &lt;div&gt;
      &lt;h1&gt;Home Component&lt;/h1&gt;
      &lt;ChildComponent /&gt;
    &lt;/div&gt;
  );
}

export default Home;

import React, { useState, useEffect } from &#39;react&#39;;

function ChildComponent() {
  const [data, setData] = useState([]);

  useEffect(() =&gt; {
    fetch(&#39;https://api.example.com/data&#39;)
      .then(response =&gt; response.json())
      .then(data =&gt; setData(data))
      .catch(error =&gt; console.error(&#39;Error fetching data:&#39;, error));
  }, []);

  return (
    &lt;div&gt;
      &lt;h2&gt;Child Component&lt;/h2&gt;
      &lt;ul&gt;
        {data.map(item =&gt; (
          &lt;li key={item.id}&gt;{item.name}&lt;/li&gt;
        ))}
      &lt;/ul&gt;
    &lt;/div&gt;
  );
}

export default ChildComponent;

答案1

得分: 1

React 严格模式 会两次渲染组件以检查函数逻辑是否正确实现。

在这种情况下,每次渲染时 API 调用都会不断增加。

您需要实施一种逻辑,以在组件销毁并第二次渲染时清理/销毁 API 调用。

有关更多信息,请参阅文档在开发中修复双重渲染引发的错误

英文:

React Strict Mode renders the component twice to check if the function logic is implemented correctly.

In this scenario, Every time you render, the API calls keeps on increasing.

You need to implement a logic to cleanup/ destroy the API call during the component getting destroyed and rendering for second time.

Refer to the docs Fixing bugs found by double rendering in development for more information.

答案2

得分: 0

我看到你的问题,这里有几个解决方案,让我们看看是否可以应用到你的项目中:

  • 首先,请确保你已关闭了ReactJS的StrictMode。
  • 其次,如果你的每个选项卡调用不同的API,请在主页上创建一个useState。
function Home() {
  const [tabs, setTabs] = useState('TOP_PAGE')
  return (
    <div>
      <h1>Home Component</h1>
      {tabs === 'TOP_PAGE' && <ChildComponentTopPage /> }
      {tabs === 'SECOND_PAGE' && <ChildComponentSecondPage /> }
    </div>
  );
}
  • 最后,如果每个子组件的API都相同,我建议将API放在父组件中,然后将props传递给每个子组件。
英文:

I see your issue, I have a few solutions here, let's see if any of them can be applied to your project:

  • Firstly, please make sure you have turned off ReactJS's StrictMode.
  • Secondly, if each tab of yours calls a different API, create a useState on the Home page.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

function Home() {
  const [tabs, setTabs] = useState(&#39;TOP_PAGE&#39;)
  return (
    &lt;div&gt;
      &lt;h1&gt;Home Component&lt;/h1&gt;
      {tabs === &#39;TOP_PAGE&#39; &amp;&amp; &lt;ChildComponentTopPage /&gt; }
      {tabs === &#39;SECOND_PAGE&#39; &amp;&amp; &lt;ChildComponentSecondPage /&gt; }
    &lt;/div&gt;
  );
}

<!-- end snippet -->

  • Lastly, if the API for each child component is the same, I recommend placing the API in the parent component, and then passing props to each child component.

答案3

得分: 0

尝试在你的 main.jsx 中移除 React.StrictMode。从:

<React.StrictMode>
<App/>
</React.StrictMode>

改为:

<App/>

因为 React.StrictMode 似乎会执行两次。

英文:

Try removing React.Strictmode in your main.jsx.
you would go from:

&lt;React.StrictMode&gt;
&lt;App/&gt;
&lt;/React.StrictMode&gt;

to:
&lt;App/&gt;

, because React.Strictmode executes twice for some reason

huangapple
  • 本文由 发表于 2023年8月9日 16:38:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865972-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定