0

I have a react query that wraps on of my API calls. I would like to expose a paramter to the user of my custom hook which lets them set the paramter for this specific API call.

How can I do that idiomatically?

My current custom hook looks like this:

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;

This doesn't expose the date parameter as I would want it, since there is no external way of modifyin that date parameter.

1 Answer 1

0

You can just pass the parameter in braces, just like functions. Then user would be able to use it like useGamesApi(key)

Sign up to request clarification or add additional context in comments.

2 Comments

I feel dumb... Thanks!
the original way of extracting the date from the queryKey should work as well. what do you mean with "it doesn't expose the date parameter as I would want it" ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.