【问题标题】:react query with parameters使用参数响应查询
【发布时间】:2022-09-23 02:05:50
【问题描述】:

我有一个包含我的 API 调用的反应查询。我想向我的自定义钩子的用户公开一个参数,让他们为这个特定的 API 调用设置参数。

我怎么能习惯性地做到这一点?

我当前的自定义挂钩如下所示:

const useGamesApi = () => {
  const [games, setGames] = useState<Game[]>([]);

  const upcomingGamesQuery = useQuery(
    [\"upcoming\", date],
    async ({ queryKey }) => {
      const [_, date] = queryKey;

      const ret = await apiGetUpcomingGames(date);
      return ret;
    },
    {
      onSuccess: (data) => {
        setGames(data);
      },
    }
  );

  return {
    games: games,
  };
};

export default useGamesApi;

这不会像我想要的那样公开日期参数,因为没有外部方法可以修改该日期参数。

    标签: react-native react-hooks react-query


    【解决方案1】:

    您可以只在大括号中传递参数,就像函数一样。然后用户就可以像 useGamesApi(key) 一样使用它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      相关资源
      最近更新 更多