一个组件中的多个 Apollo 查询,获取无法重新声明块范围变量

问题描述 投票:0回答:1

我在一个 React 组件中有这两个 Apollo 查询:

 const { data, loading, error } = useQuery(PlaylistQuery, {
    variables: { _id: playlistId },
    fetchPolicy: "cache-and-network",
  });

  const [loadSong, { called, loading, error, data }] = useLazyQuery(SongQuery, {
    variables: {
      _id: song._id,
    },
  });

这显然会给我这个错误信息:

Cannot redeclare block-scoped variable

解决这个问题的聪明方法是什么?

javascript reactjs graphql apollo apollo-client
1个回答
4
投票

为避免此问题,您可以简单地重命名查询的变量。例如,如下所示:

const {
  data: playListData,
  loading: playListLoading,
  error: playListError,
} = useQuery(PlaylistQuery, {
  variables: { _id: playlistId },
  fetchPolicy: "cache-and-network",
});

const [
  loadSong,
  { called: songFuctionCalled, loading: songLoading, error: songError, data: songData },
] = useLazyQuery(SongQuery, {
  variables: {
    _id: song._id,
  },
});
© www.soinside.com 2019 - 2024. All rights reserved.