What is gatsby-plugin-react-helmet?
The gatsby-plugin-react-helmet package is a Gatsby plugin that integrates React Helmet into your Gatsby site. React Helmet is a library that allows you to manage changes to the document head, such as the title, meta tags, and other elements, from within your React components.
What are gatsby-plugin-react-helmet's main functionalities?
Setting the Page Title
This feature allows you to set the title of the page dynamically from within a React component. The title tag is updated in the document head when the component is rendered.
import React from 'react';
import { Helmet } from 'react-helmet';
const MyComponent = () => (
<div>
<Helmet>
<title>My Page Title</title>
</Helmet>
<h1>Hello, world!</h1>
</div>
);
export default MyComponent;
Adding Meta Tags
This feature allows you to add meta tags to the document head. You can specify various meta tags such as charset, description, keywords, etc., directly within your React components.
import React from 'react';
import { Helmet } from 'react-helmet';
const MyComponent = () => (
<div>
<Helmet>
<meta charSet="utf-8" />
<meta name="description" content="This is a description of my page" />
</Helmet>
<h1>Hello, world!</h1>
</div>
);
export default MyComponent;
Link Tags
This feature allows you to add link tags to the document head. This is useful for things like setting the canonical URL of the page, adding stylesheets, or preloading resources.
import React from 'react';
import { Helmet } from 'react-helmet';
const MyComponent = () => (
<div>
<Helmet>
<link rel="canonical" href="https://www.example.com/my-page" />
</Helmet>
<h1>Hello, world!</h1>
</div>
);
export default MyComponent;
Other packages similar to gatsby-plugin-react-helmet
react-helmet
React Helmet is the core library that gatsby-plugin-react-helmet integrates with. It provides the same functionality for managing changes to the document head in any React application, not just Gatsby.
react-meta-tags
React Meta Tags is another library for managing the document head in React applications. It offers similar functionality to React Helmet but with a different API and some additional features like support for nested tags.
gatsby-plugin-react-helmet
Provides drop-in support for server rendering data added with
React Helmet.
React Helmet is a component which lets you control your document head using
their React component.
With this plugin, attributes you add in their component, e.g. title, meta
attributes, etc. will get added to the static HTML pages Gatsby builds.
Install
npm install --save gatsby-plugin-react-helmet react-helmet
How to use
Just add the plugin to the plugins array in your gatsby-config.js
plugins: [`gatsby-plugin-react-helmet`];