Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

335
Visualizações
React Query: How to invalidate from mutation?

I am trying to refetch a specific data, which should update after a post request in another API. It's worth mentioning that based on that request two APIs should update and one of them updates, another one not.

I've tried multiple ways to refetch the API based on successful post request, but nothing seems to work.
I tried to also add 2nd option like this, but without success.

 {
   refetchInactive: true,
   refetchActive: true,
 }

As it mentioned in this discussion, maybe "keys are not matching, so you are trying to invalidate something that doesn't exist in the cache.", but not sure that it's my case.

What is more interesting is that clicking the invalidate button in the devtools, the invalidation per se works.

Mutation function:

import { BASE_URL, product1Url, product2Url } from './services/api'
import axios from 'axios'
import { useMutation, useQueryClient } from 'react-query'

export const usePostProduct3 = () => {
  const queryClient = useQueryClient()

  const mutation = useMutation(
    async (data) => {
      const url = `${BASE_URL}/product3`
      return axios.post(url, data).then((response) => response)
    },
    {
      onSuccess: async (data) => {
        return (
          await queryClient.invalidateQueries(['prodKey', product1Url]), //this one doesn't work
          queryClient.invalidateQueries(['prodKey', product2Url]) //this api refetch/update works
        )      },
    },
  )

  return mutation
}

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda