简体   繁体   English

如何防止组件在反应中重新渲染?

[英]How to prevent component from re-rendering in react?

The content component and it takes the API as a argument from another component.内容组件,它将 API 作为另一个组件的参数。 But when it takes a new input and the API updates it re-renders(i think twise) and the result array is empty.但是当它需要一个新的输入并且 API 更新它时,它会重新渲染(我认为是 twise)并且结果数组是空的。 That is the problem.那就是问题所在。 Here is the code.这是代码。

    import React from 'react';
import  { useState,useEffect } from 'react';
import ReactDOM from 'react-dom';

const Content=(api)=>{

  const [cities, setItems] = useState([]);

  const API=api;
  console.log(API);

  
  useEffect(() => {
  //  setItems([]);  //clearing cities array.deleting previuos data from the array
    fetch(API)
      .then(res => res.json(console.log(res)))
      .then(
        (result) => {
          console.log(cities)
          setItems(cities => cities.concat(result));
          
        },
      )
  },[API]);

  console.log(cities)

    return (
        <ul>
        {cities.map(citi => (
          <li key={citi.city.id}>
            The city is is{citi.city.id} 
            The city name is{citi.city.name}
            The temparature is {citi.list[0].main.temp}
          </li>
        ))}
      </ul>
  
      
    )
  }


export default Content;

The first log is returning an empty array.第一个日志返回一个空数组。 But the second log is returning the array with correct results Here is the console output但是第二个日志返回了正确结果的数组 这是控制台output

As you can see it renders twice and the final array is an empty one.正如你所看到的,它渲染了两次,最终的数组是一个空的。 So my issue is that the final result array is an empty array even id the api is fetching data.所以我的问题是最终结果数组是一个空数组,即使是 api 正在获取数据。 Because of that this component is not returning anything.因为这个组件没有返回任何东西。 How to fix that.如何解决。 The output is showing that the array is empty output 显示数组为空

please don't downvote.Just leave a comment if this needs an improvement请不要投票。如果需要改进,请发表评论

I am assuming that the API you have mentioned here is props and it should be props.api.我假设你在这里提到的 API 是道具,它应该是 props.api。 The answer is using the api in the Content component after destructuring from props答案是在从道具解构后在 Content 组件中使用 api

import React from 'react';
import  { useState,useEffect } from 'react';
import ReactDOM from 'react-dom';

const Content=({api})=>{

  const [cities, setItems] = useState([]);

  useEffect(() => {
    if(api){
        fetch(api)
           .then(res => res.json())
           .then(result => {
                // add some guarding condition here for result
                let updatedCities = cities.concat(result);
                setItems(updatedCities);
           })
    }
  },[api]);

  console.log(cities)

    return (
        <ul>
        {cities.map(citi => (
          <li key={citi.city.id}>
            The city is is{citi.city.id} 
            The city name is{citi.city.name}
            The temparature is {citi.list[0].main.temp}
          </li>
        ))}
      </ul>     
    )
  }
export default Content;

This would only re-render when the API has changed.这只会在 API 发生更改时重新渲染。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM