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

198
Visualizações
Render router query parameters on first render

Problem

In my nextjs typescript setup I have a dynamic route in a file called [name].tsx. Based on the name I want to prefll meta tags. React router can retrieve the value stored in the name parameter but the only after rendering my component with name = undefined. This causes the meta tags to contain name = undefined as well. How can I fill the meta tags with the correct value on the first render?

Code

import { useEffect } from "react";
import { useRouter } from "next/router";

function Page() {
  const router = useRouter();
  const { name } = router.query;
  useEffect(() => {
    if (!name) {
      return;
    }
  }, [name]);

  return (
    <head>
      <meta property="og:description" content={"My name is " + name} />
    </head>
  );
}

export default Page;

Test

Enter the URL of the page including the route here: https://cards-dev.twitter.com/validator

Some say this isn't possible. Is this true?

Solutions that don't work for me:

  1. There used to be componentWillMount, but it's deprecated and UNSAFE_componentWillMount doesn't work for me. (`componentWillMount` warnings visible even though 'componentWillMount' is not explicitly used)
  2. Using a hook instead of componentWillMount doesn't work for me (first render still name = undefined) How to use componentWillMount() in React Hooks?
  3. Render null if name undefined - doesn't prevent initial render.
  4. Maybe there is a way to achieve this using react context? useRouter/withRouter receive undefined on query in first render (answer by Hunter Medney)
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