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

126
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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