question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

null is passed as pageParam with PersistQueryClientProvider

See original GitHub issue

Describe the bug

Our code uses react-native, useInfiniteQuery with PersistQueryClientProvider:

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 5 * 60 * 1000, // 5 min
      cacheTime: 1000 * 60 * 60 * 24, // 24 hours
    },
  },
});

export function CustomQueryClientProvider({ children }: { children: ReactNode }) {
  return (
    <PersistQueryClientProvider
      client={queryClient}
      persistOptions={{ persister: asyncStoragePersister }}
      onSuccess={() => {
        // resume mutations after initial restore from the storage was successful
        queryClient.resumePausedMutations();
      }}
    >
      {children}
    </PersistQueryClientProvider>
  );
}

type MovesQueryKeyType = [string, GetMovesFilter | undefined];
type MovesQueryOptions = UseInfiniteQueryOptions<MovesData, unknown, MovesData, MovesData, MovesQueryKeyType>;
type MovesQueryFunctionContext = QueryFunctionContext<MovesQueryKeyType, number>;

export const useMovesQuery = (filter?: GetMovesFilter, options?: MovesQueryOptions) =>
  useInfiniteQuery(
    [MOVE_QUERY_KEY, filter],
    ({ signal, pageParam }: MovesQueryFunctionContext) => getMoves(filter, pageParam, signal),
    {
      ...options,
      getNextPageParam: (_, pages) => pages.length + 1,
    }
  );
  
const { isLoading, data: movesData } = useMovesQuery(undefined, {
    staleTime: 0,
  });

According to the type system pageParam passed to getMoves is a number | undefined. But when I re-run my app null is passed as pageParam

Your minimal, reproducible example

in description

Steps to reproduce

  1. Open app, load first page of the infinite query
  2. Reopen app to allow PersistQueryClientProvider to restore persisted cache

Expected behavior

null should not be passed as pageParam according to the types

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Expo Go on Android

react-query version

4.10.1

TypeScript version

latest

Additional context

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

2reactions
TkDodocommented, Oct 28, 2022

We’ve discussed this and there doesn’t seem to be a good way that wouldn’t break some edge cases where people rely on null being rightfully returned from getNextPageParam.

We have a plan that disallows null being returned from there, then we can treat null like undefined and side-step that issue. We would make sure internally that you’d always get undefined passed into the queryFn even if null sneaks into the pageParams.

But, it will have to wait a bit for the next major release.

1reaction
TkDodocommented, Oct 22, 2022

The problem is that pageParams will likely have multiple entries, so it’s:

pageParams: [undefined, 5, 10, 15]

there is no real way to serialize this to json and keep the information that there was an entry.

superjson supports serializing undefined, so you might just want to use this as a drop-in replacement for JSON.stringify / JSON.parse

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I pass null as a query key? · Discussion #2011 - GitHub
I use SWR and I'm considering switching to React Query. Basically everything about react-query seems better so far, except one: null / error ......
Read more >
React query infinite scroll pageParams is undefined
for the first page, the cursor will be set to 0 , as pageParam is undefined . So it's just up to you...
Read more >
How to Handle null values when passing nothing in Single ...
In this post we will see how to handle NULL input (-- input value in single select parameter) in single select parameter. i.e.,...
Read more >
Passing nulls to Named Query when testing - Ignition
I have an SQL table which has nullable numerical fields (and none of the fields have default values) , and I want to...
Read more >
SQL ignore part of WHERE if parameter is null - OutSystems
I'm trying an SQL query where I pass a parameter (MovieID). This parameter has an Identifier Type. This parameter may be empty. What...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found