Skip to content

Show loading only for the first time #8333

Answered by TkDodo
chertik77 asked this question in Q&A
Discussion options

You must be logged in to vote

when your queryKey changes, you’re going to observe a new query that doesn’t have any previous data, so you’ll get a hard loading state. To continue observing data from previous cache entries, we have placeholderData:

  return useQuery({
    queryKey: ['board', boardId, cardPriority],
    queryFn: () =>
      boardService.getBoardById(boardId!, { priority: cardPriority }),
    enabled: !!boardId,
    placeholderData: (prev) => prev
  })

You can also import keepPreviousData from @tanstack/react-query and set:

placeholderData: keepPreviouData

if you prefer that. it does the same thing.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@chertik77
Comment options

@chertik77
Comment options

@TkDodo
Comment options

@chertik77
Comment options

@TkDodo
Comment options

Answer selected by chertik77
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants