The <meta>
tags always inside the <head> element and meta tags always passed as
name/value pairs
Source URL: https://www.npmjs.com/package/react-helmet
React Helmet takes plain HTML tags and outputs plain HTML tags. React Helmet is very simple
and friendly for beginner react developers.
Reactjs component will manage all of your changes
to the document head.
Helmet will support following attributes for
body:
1. html
2. title
tags
Helmet will support of the following valid head
tags:
1. title
2. base
3. meta
4. link
5. script
6. no-script
7. style
tags
Helmet will support server-side rendering.
Helmet will supports nested components override
duplicate head changes.
See
the below examples step by step,
Example
1:
Steps
1
- install NPM commands
npm install react-helmet --save
Use Helmet Tags in your app componet:
Nested or other components will override duplicate changes:
import React from "react";
import { Helmet } from "react-helmet";
class Application extends React.Component {
render() {
return (
<div className="application">
<Helmet>
<meta charSet="utf-8" />
<title>Your Title Here..</title>
<link rel="canonical" href="https://code-sample.com/example" />
<meta property="og:site_name" content="code sample" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:domain" content="code-sample.com" />
<meta name="twitter:title" property="og:title" itemprop="name"
content="react helmet meta tag dynamically." />
<meta name="twitter:description"
property="og:description"
itemprop="description"
content="I am trying to explain how to create meta tag from
awebsite dynamically using react-helmet" />
</Helmet>
...
</div>
);
}
};
Nested or other components will override duplicate changes:
<Parent>
<Helmet>
<title>My Title</title>
<meta name="description" content="Your Helmet description" />
<meta property="og:site_name" content="code sample" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:domain" content="code-sample.com" />
<meta name="twitter:title" property="og:title" itemprop="name"
content="react helmet meta tag dynamically." />
<meta name="twitter:description" property="og:description"
itemprop="description"
content="I am trying to explain how to create meta tag from awebsite
dynamically using react-helmet" />
</Helmet>
<Child>
<Helmet>
<title>Nested Title</title>
<meta name="description" content="Your Nested component description" />
</Helmet>
</Child>
</Parent>
The
outputs look like:
<head>
<title>Nested Title</title>
<meta name="description" content="Your Nested component description">
<meta property="og:site_name" content="code sample" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:domain" content="code-sample.com" />
<meta name="twitter:title" property="og:title" itemprop="name"
content="react helmet meta tag dynamically." />
<meta name="twitter:description" property="og:description" itemprop="description"
content="I am trying to explain how to create meta tag from awebsite
dynamically using react-helmet" />
</head>
Source URL: https://www.npmjs.com/package/react-helmet
For
the Server Usage –
To use on the server, call Helmet.renderStatic()
after ReactDOMServer.renderToString or ReactDOMServer.renderToStaticMarkup to
get the head data for use in your prerender.
As an Example for Server Usage - https://www.npmjs.com/package/react-helmet
Download source code from GitHub
Download source code from GitHub
Example
2
- Explore this Add/Update Meta Tags in React