最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Vue components keep the old state even when replaced - Stack Overflow

programmeradmin4浏览0评论
  const queryKey = ref("userData");

  const { data, isLoading, error } = useQuery({
    queryKey: [queryKey], // Chave única para identificar a consulta
    queryFn: async () => {
      const response = await makeReq.get(`users/data`, {
        withCredentials: true,
      });
      return response.data; // Retornando os dados da resposta
    },
  });

  const logout = async () => {
    try {
      const result = await Swal.fire({
        title: "Tem certeza que deseja sair?",
        text: "Você terá que refazer o login",
        icon: "warning",
        showCancelButton: true,
        confirmButtonText: "Sim",
      });

      if (result.isConfirmed) {
        await makeReq.post("users/logout", {}, { withCredentials: true });
        queryKey.value = "";

        setTimeout(() => {
          router.push("/");
        }, 1000);
      }
    } catch (err) {
      Swal.fire({
        title: "Ops... Erro interno",
        text: "Volte mais tarde",
        icon: "error",
        showCancelButton: false,
        confirmButtonText: "Voltar",
      });
      console.log(err);
    }
  };

The code above would be a piece of what would be my header, I use this component in some places in my application, my intention is whenever the user logs out through my logout function it throws it to the main route "/" the problem is that when I do this the old state remains and the header does not show the data that I would like it to show.

I tried to use the query invalidation but apparently I don't understand very well how it works and even though I really believe it's right, it doesn't work...

An important detail, the code above works, however, there is obviously something wrong with it, it doesn't seem right to me to use setTimeout in this way

  const queryKey = ref("userData");

  const { data, isLoading, error } = useQuery({
    queryKey: [queryKey], // Chave única para identificar a consulta
    queryFn: async () => {
      const response = await makeReq.get(`users/data`, {
        withCredentials: true,
      });
      return response.data; // Retornando os dados da resposta
    },
  });

  const logout = async () => {
    try {
      const result = await Swal.fire({
        title: "Tem certeza que deseja sair?",
        text: "Você terá que refazer o login",
        icon: "warning",
        showCancelButton: true,
        confirmButtonText: "Sim",
      });

      if (result.isConfirmed) {
        await makeReq.post("users/logout", {}, { withCredentials: true });
        queryKey.value = "";

        setTimeout(() => {
          router.push("/");
        }, 1000);
      }
    } catch (err) {
      Swal.fire({
        title: "Ops... Erro interno",
        text: "Volte mais tarde",
        icon: "error",
        showCancelButton: false,
        confirmButtonText: "Voltar",
      });
      console.log(err);
    }
  };

The code above would be a piece of what would be my header, I use this component in some places in my application, my intention is whenever the user logs out through my logout function it throws it to the main route "/" the problem is that when I do this the old state remains and the header does not show the data that I would like it to show.

I tried to use the query invalidation but apparently I don't understand very well how it works and even though I really believe it's right, it doesn't work...

An important detail, the code above works, however, there is obviously something wrong with it, it doesn't seem right to me to use setTimeout in this way

Share Improve this question asked Feb 17 at 15:19 rodrigorodrigo 1
Add a comment  | 

1 Answer 1

Reset to default 0

try:

queryClient.invalidateQueries({ queryKey: ["userData"] }) to refetch fresh data.

and queryClient.removeQueries({ queryKey: ["userData"] }) to completely clear cached user data.

发布评论

评论列表(0)

  1. 暂无评论