Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda