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

125
Vistas
How can I use state values as Meta tag content?

I want a page link to preview an image and titles when shared. Using Nextjs Head Component for that purpose. I fetch all the necessary details on page load and use them as contents of the meta attributes.

let campaign = this.state.campaignDetails;
<React.Fragment>
            <Head>
                <title>{campaign!==null && campaign.title}</title>
                <meta name="title" content={campaign!==null && campaign.title}/>
                <meta name="description" content={campaign!==null ? campaign.description : undefined}></meta>
                <meta name="image" content={campaign!==null ? campaign.image : undefined}></meta>

                <meta property="og:title" content={campaign!==null ? campaign.title : undefined}/>
                <meta property="og:type" content="website"/>
                <meta property="og:site_name" content="website"/>
                <meta property="og:url" content="https://website.com/"/>
                <meta property="og:image" content={campaign!==null ? campaign.image : undefined}/>
                <meta property="og:description" content={campaign!==null ? campaign.description : undefined}/>

                <meta name="twitter:card" content={campaign!==null ? campaign.image : undefined}/>
                <meta name="twitter:title" content={campaign!==null ? campaign.title : undefined}/>
                <meta name="twitter:description" content={campaign!==null ? campaign.description : undefined}/>
                <meta name="twitter:site" content="@website"/>
                <meta name="twitter:image" content={campaign!==null ? campaign.image : undefined}/>
                <meta name="twitter:creator" content="@website"/>
            </Head>
            <div></div>
 </React.Fragment>

The above approach doesn't do as I require. I used react helmet to no avail. What am I missing? Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Please provide a complete code sample to be able to reproduce the problem. Supposing that you are actually returning a Next.js Head component the issue is most likely with your campaign variable, so be sure to check that campaign is not null or undefined as MD M Nauman mentioned.

import Head from 'next/head';

function YourPage() {
  const { campaign = {} } = this.state.campaignDetails; // Make sure this is not null or undefined!
  const { description, image } = campaign;

  const seo = { description, image };

  return (
    <div>
      <Head>
        <title>My page title</title>
        <meta name="description" content={seo.description} />
        <meta name="image" content={seo.image} />
        {/* ... */}
      </Head>
      <p>Hello world!</p>
    </div>
  );
}

export default YourPage;

You can also check out next-seo maybe it helps you.

about 4 years ago · Juan Pablo Isaza Denunciar
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