A document head manager for React
This reusable React component will manage all of your changes to the document head.
Helmet takes plain HTML tags and outputs plain HTML tags. It's dead simple, and React beginner friendly.
import React from "react";
import {Helmet} from "react-helmet";
class Application extends React.Component {
render () {
return (
);
}
};Nested or latter components will override duplicate changes:
<Parent>
<Helmet>
<title>My Title</title>
<meta name="description" content="Helmet application" />
</Helmet>
<Child>
<Helmet>
<title>Nested Title</title>
<meta name="description" content="Nested component" />
</Helmet>
</Child>
</Parent>outputs:
<head>
<title>Nested Title</title>
<meta name="description" content="Nested component">
</head>See below for a full reference guide.
title, base, meta, link, script, noscript, and style tags.body, html and title tags.Helmet 5 is fully backward-compatible with previous Helmet releases, so you can upgrade at any time without fear of breaking changes. We encourage you to update your code to our more semantic API, but please feel free to do so at your own pace.
Yarn:
yarn add react-helmetnpm:
npm install --save react-helmetTo use on the server, call Helmet.renderStatic() after ReactDOMServer.renderToString or ReactDOMServer.renderToStaticMarkup to get the head data for use in your prerender.
Because this component keeps track of mounted instances, you have to make sure to call renderStatic on server, or you'll get a memory leak.
ReactDOMServer.renderToString(<Handler />);
const helmet = Helmet.renderStatic();This helmet instance contains the following properties:
basebodyAttributeshtmlAttributeslinkmetanoscriptscriptstyletitleEach property contains toComponent() and toString() methods. Use whichever is appropriate for your environment. For attributes, use the JSX spread operator on the object returned by toComponent(). E.g:
const html = `
<!doctype html>
<html ${helmet.htmlAttributes.toString()}>
<head>
${helmet.title.toString()}
${helmet.meta.toString()}
${helmet.link.toString()}
</head>
<body ${helmet.bodyAttributes.toString()}>
</body>
</html>
`;function HTML () {
const htmlAttrs = helmet.htmlAttributes.toComponent();
const bodyAttrs = helmet.bodyAttributes.toComponent();
return (
<html {...htmlAttrs}>
<head>
{helmet.title.toComponent()}
{helmet.meta.toComponent()}
{helmet.link.toComponent()}
</head>
<body {...bodyAttrs}>
</body>
</html>
);
}If you are using a prebuilt compilation of your app with webpack in the server be sure to include this in the webpack file so that the same instance of react-helmet is used.
externals: ["react-helmet"],Or to import the react-helmet instance from the app on the server.
…Please take a moment to review the guidelines for contributing.
MIT
Warning 'UNSAFE_componentWillMount', How to fix the problem?
[testing] Helmet.peek() returns previous elements in unit testing
React-helmet not working with latest react version along with react hooks.
Order of stylesheets changing
Adding external javascript script to React failed!
Body style attribut is not set properly
TS2786: 'Helmet' cannot be used as a JSX component
is it safe?
Server-side title generation is not working as expected on the browser
Order of rel and href matters for link tags with rel="alternate"