Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include meta tags dynamically in react js?

I am using reactjs , i want to share my content in facebook,it is happening but after sharing it is not showing images,title,description of the content. so I used react-helmet to dynamically add meta tags in index.html.

<Helmet>
<meta property="og:type" content="video.other" />
<meta property="og:image"       content="https://www.w3schools.com/css/trolltunga.jpg" />
<meta property="og:title" content="My Title" />
<meta property="og:url" content="https://www.afnity.com/video/155" />
<meta property="og:description" content="some discription of the shared    content" />

</Helmet>

and here is the share button

   <button> <a title="dummy" 
href="http://www.facebook.com/sharer.php? u=https://dummy.com/videos/id=25"     target="_blank">
share</a>
</button>

but it is not working.

like image 522
nik7 Avatar asked Mar 28 '17 14:03

nik7


People also ask

What is dynamic meta description?

The meta description is a short text preview displayed for each search result that summarizes a page's content. Search engines show the meta description when it's highly relevant to what the user is searching otherwise they might pick text from the page and create a dynamic description.

What is dynamic in react JS?

As the building blocks of React applications, components are dynamic, in that they can describe a template of HTML and fill in variable data. This lesson builds a real example of a blogging application to illustrate dynamic components.


1 Answers

This can be achieved using react-document-meta npm module when server side rendering enabled in your react application

const meta = {
      title: 'Samvikshana - New Perspective of Exploration',
      meta: {
        property: {
          'og:title': 'Samvikshana',
          'og:url': 'https://samvikshana.weebly.com/',
          'og:image': imageUri,
          'og:description': 'New Perspective of Exploration',

          'twitter:card': 'summary_large_image',
          'twitter:title': 'Samvikshana',
          'twitter:description': 'New Perspective of Exploration',
          'twitter:image': imageUri
        }
      }
    };

    return (
      <div className='container-fluid'>
        <DocumentMeta {...meta} />
         </div>);
    }

Read this blog for more information - https://samvikshana.weebly.com/blog/dynamic-meta-tags-in-react-components

like image 106
Pavan Avatar answered Oct 02 '22 11:10

Pavan