React Helmet
React Helmet is a versatile React component designed to manage and manipulate the document head (`<head>`) of web pages. It enables developers to declaratively define metadata like titles, meta tags, link tags, script tags, and more directly within their component tree. The current stable version is 6.1.0, which re-introduced the default export pattern alongside the named export. While there's no strict release cadence, the project is actively maintained with updates addressing compatibility and feature enhancements. A key differentiator is its straightforward API that accepts plain HTML tags and its ability to handle server-side rendering, ensuring that the correct head elements are generated for initial page loads. It automatically handles nested components, where later or deeper components override duplicate head changes, simplifying SEO and page-specific metadata management.
Common errors
-
TypeError: (0, react_helmet_1.default) is not a function
cause Attempting to use `import Helmet from 'react-helmet'` when on `react-helmet@6.0.0`, which only supported named exports.fixChange your import statement to `import { Helmet } from 'react-helmet'`. If you are on `6.1.0` or later, both default and named imports are supported, so check your package version. -
Memory leak reported in server-side rendering environment.
cause The `Helmet.renderStatic()` method was not called after `ReactDOMServer.renderToString` or `ReactDOMServer.renderToStaticMarkup` on the server.fixAfter rendering your React application to a string, add `const helmet = Helmet.renderStatic();` to clear Helmet's state and prevent memory accumulation across requests. -
Warning: Using deprecated Helmet API. Consider migrating to the new API.
cause You are passing props like `titleTemplate` directly to the Helmet component, which is part of the older API that will be deprecated.fixRefactor your Helmet usage to pass plain HTML tags as children. For example, instead of a `titleTemplate` prop, define the full `<title>` tag content directly.
Warnings
- breaking Version 6.0.0 removed the default export for Helmet, requiring a named import `import { Helmet } from 'react-helmet'`. This was re-introduced in v6.1.0, allowing `import Helmet from 'react-helmet'` again, but users on exactly v6.0.0 will encounter issues with default imports.
- gotcha When performing server-side rendering (SSR), it is crucial to call `Helmet.renderStatic()` after `ReactDOMServer.renderToString` or `ReactDOMServer.renderToStaticMarkup`. Failure to do so will result in a memory leak on the server as Helmet keeps track of mounted instances.
- deprecated In v5.0.0, a new simplified API was introduced. Directly passing Helmet props like `titleTemplate`, `defaultTitle`, `onChangeClientState`, and `encodeSpecialCharacters` is still backward-compatible but will be deprecated in future versions. The recommended approach is to use plain HTML tags as children.
Install
-
npm install react-helmet -
yarn add react-helmet -
pnpm add react-helmet
Imports
- Helmet
import { Helmet } from 'react-helmet'import Helmet from 'react-helmet'
- Helmet (named)
import Helmet from 'react-helmet'
import { Helmet } from 'react-helmet' - Helmet.renderStatic
const helmet = Helmet.renderStatic(); // without calling ReactDOMServer.renderToString first
import { Helmet } from 'react-helmet'; /* ... renderToString ... */ const helmet = Helmet.renderStatic();
Quickstart
import React from "react";
import { Helmet } from "react-helmet";
function MyPage() {
return (
<div className="application">
<Helmet>
<meta charSet="utf-8" />
<title>My Awesome Page Title</title>
<link rel="canonical" href="https://mysite.com/current-page" />
<meta name="description" content="A detailed description of my awesome page." />
<meta property="og:title" content="Open Graph Title" />
<meta property="og:description" content="Open Graph Description" />
<meta property="og:image" content="https://mysite.com/image.jpg" />
</Helmet>
<h1>Welcome to my application!</h1>
<p>This content is part of the page body.</p>
</div>
);
}
export default MyPage;