为什么 “res” 被记录两次?

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

Can anyone explain.Why the 'res" is getting logged two times?

问题

我试图从Flickr获取图像数据。我已经收到了响应,但无法显示图像。我尝试使用useEffect来获取API并使用useState变量来存储图像数组。但仍然被调用了两次。

const DisplayImages = () => {
    let photos = [];
    fetch(
      `https://www.flickr.com/services/rest/?method=flickr.galleries.getPhotos&api_key=***********************&gallery_id=************&format=json&nojsoncallback=1`
    )
      .then((response) => response.json())
      .then((res) => {
        const images = res;
        console.log(res);
        photos = images.photos.photo;
      });

  return (
    <>
      <div className="imagescontainer">
        {photos.map((photo) => {
            console.log(photo);
          let url = `https://farm${photo.farm}.staticflickr.com/${photo.server}/${photo.id}_${photo.secret}`;
          <>
            {/* <img src={require(url)} alt="img" /> */}
          </>
        })}
      </div>
    </>
  );
};

我期望图像被呈现。

英文:

I am trying to get the images data from flickr. I am getting the response but not able to display the image.I have tried using useEffect to fetch the api and useState variable to store images array. Still it was getting called two times.

const DisplayImages = () =&gt; {
    let photos = [];
    fetch(
      ` https://www.flickr.com/services/rest/?method=flickr.galleries.getPhotos&amp;api_key=***********************&amp;gallery_id=************&amp;format=json&amp;nojsoncallback=1`
    )
      .then((response) =&gt; response.json())
      .then((res) =&gt; {
        const images = res;
        console.log(res);
        photos = images.photos.photo;
      });

  return (
    &lt;&gt;
      &lt;div className=&quot;imagescontainer&quot;&gt;
        {photos.map((photo) =&gt; {
            console.log(photo);
          let url = `https://farm${photo.farm}.staticflickr.com/${photo.server}/${photo.id}_${photo.secret}`;
          &lt;&gt;
            {/* &lt;img src={require(url)} alt=&quot;img&quot; /&gt; */}
          &lt;/&gt;
        })}
      &lt;/div&gt;
    &lt;/&gt;
  );
};

I expected the images to be rendered.

答案1

得分: 1

因为 React 可能配置为 严格模式(应该是这样),所以它被调用两次。在开发中,严格模式故意导致 hooks 被调用两次。这是有意的,你需要在编写 React 组件时考虑到这一点。

其次,你正在使用一个局部作用域变量 photos 来保存状态,而你需要使用 useState 实例,否则 React 不会知道状态何时更改并重新渲染组件。

第三,看起来你正在将 photos,它初始化为空数组,设置为单个照片实例。

英文:

It's getting called twice because React is probably configured to be in strict mode (as it should be). In development, strict mode causes hooks to be called twice on purpose. This is intentional and you'll need to write your React components with this in mind.

Second, you're using a locally-scoped variable photos to hold state, when you need to use useState instance, otherwise React won't know when the state changes and re-render the component.

Third, it looks like you're setting photos, which is initialized with an empty array, to a single instance of a photo.

huangapple
  • 本文由 发表于 2023年2月19日 21:51:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500592.html
匿名

发表评论

匿名网友

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

确定