小编lay*_*thq的帖子

当道具依赖项更改时,React useEffect 钩子不会触发

我正在使用 useEffect 挂钩根据photos传入的道具中的数据进行 API 调用。

const ImageCarousel = ({ src, photos }) => {
  const [photoList, setPhotos] = useState([]);
  const storageRef = firebase.storage().ref();
  console.log("photos prop:", photos);

  const getImage = photoId => {
    return new Promise((resolve, reject) => {
      storageRef
        .child("images/" + photoId)
        .getDownloadURL()
        .then(url => resolve(url))
        .catch(error => reject(error));
    });
  };

  useEffect(() => {
    console.log("trigger");
    Promise.all(
      photos.map(async photoId => {
        const url = await getImage(photoId);
        return url;
      })
    )
      .then(photoUrls => {
        setPhotos(photoUrls);
      })
      .catch(e => console.log(e));
  }, …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-hooks

2
推荐指数
1
解决办法
4934
查看次数

标签 统计

javascript ×1

react-hooks ×1

reactjs ×1