Using query router with Nextjs

Asked

Viewed 66 times

0

I’m creating a referral link to my site (someone will give this link to someone else, who will get a discount), when I use the link in environment on my machine, it usually works the router.query, when I use in staging environment, apparently it performs 2 times the function, on the first Undefined handle, on the second correctly, but does not work the route.

Do you have any idea what it might be?

const Referal = ({ onAddReferal }) => {
  const router = useRouter();
  const { cod } = router.query;

  console.warn(router.query);
  console.warn(cod);

  useEffect(() => {
    if (!isEmpty(cod)) {
      console.warn('deu certo');
      onAddReferal(cod);
    } else {
      console.warn('n deu certo');
      router.push('/');
    }
  }, []);

  return null;
};

inserir a descrição da imagem aqui

PS: I added a lot of console.warn, to see if I could find something

  • I found a solution by vercel, pq according to them, if I’m using getInitialProps, it runs Hydrate before, because useRouter is a hook state (https://github.com/vercel/next.js/discussions/11484). I tried to change the code using const cod = router.query['cod']; unsuccessful.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.