useQuery not updating after query key changing in custom hook

Hi all, thank you for your time.
I'm composing a custom hook to retrieve an array in this manner. I realized that destructured data returning from useQuery was not being updated on query key changes when pageUp or down is called. Could anyone point out my mistake?

import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useAuthContext } from '../AuthContextProvider.tsx';
import { fetchPaginatedListings } from '../queries/actionButtonQueries.ts';
import { useState } from 'react';

const useActionButtonsData = () => {
  const queryClient = useQueryClient();
  const key = 'actionButtons';
  const [page, setPage] = useState(1);

  async function pageUp() {
    setPage((last) => last + 1);
  }

  async function pageDown() {
    setPage((last) => {
      if (last > 1) {
        return last - 1;
      } else {
        return last;
      }
    });
  }

  const { data, isLoading, isError, error } = useQuery({
    queryKey: [key, page],
    queryFn: async () => {
      const data = await fetchPaginatedListings({ page: page, limit: 9 });
      return data;
    },
  });
  return {
    data,
    isLoading,
    isError,
    error,
    pageUp,
    pageDown,
  };
};

export default useActionButtonsData;
Was this page helpful?