+0
日增长
17,435
stars
639
forks
105
watchers
315
prs
390
issues
3
releases
README
React Helmet
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.
6.1.0 Major Changes
Example
javascript
import React from "react";
import {Helmet} from "react-helmet";
class Application extends React.Component {
render () {
return (
My Title
...
);
}
};
Nested or latter components will override duplicate changes:
javascript
My Title
Nested Title
outputs:
html
Nested Title
See below for a full reference guide.
Features
- Supports all valid head tags: title, base, meta, link, script, noscript, and style tags.
- Supports attributes for body, html and title tags.
- Supports server-side rendering.
- Nested components override duplicate head changes.
- Duplicate head changes are preserved when specified in the same component (support for tags like "apple-touch-icon").
- Callback for tracking DOM changes.
Compatibility
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.
Installation
Yarn:
bash
yarn add react-helmet
npm:
bash
npm install --save react-helmet
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.
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.
javascript
ReactDOMServer.renderToString();
const helmet = Helmet.renderStatic();
This helmet instance contains the following properties:
- base
- bodyAttributes
- htmlAttributes
- link
- meta
- noscript
- script
- style
- title
Each 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:
As string output
javascript
const html =
${helmet.title.toString()}
${helmet.meta.toString()}
${helmet.link.toString()}
// React stuff here
;
As React components
javascript
function HTML () {
const htmlAttrs = helmet.htmlAttributes.toComponent();
const bodyAttrs = helmet.bodyAttributes.toComponent();
return (
{helmet.title.toComponent()}
{helmet.meta.toComponent()}
{helmet.link.toComponent()}
// React stuff heTop issues
#79any possible way to disable 'data-react-helmet="true"' from server side rendering?69 comments · 2015-12-02#216Is the `rewind` approach safe?33 comments · 2016-12-28#548Stop using `UNSAFE_componentWillMount`31 comments · 2020-05-02#341Existing meta tags don't get overriden26 comments · 2017-12-14#189Title update lag26 comments · 2016-11-05#646TS2786: 'Helmet' cannot be used as a JSX component22 comments · 2021-07-07#146How do I know the scripts are ready?22 comments · 2016-05-22#374How to use react-helmet to add twitter card meta tag for dynamic data 21 comments · 2018-05-07
Latest release
v6.1.0 6.1.0 2020-06-08Features
Re-add Helmet as a default export (547)
Note: You may now choose import Helmet or import {Helmet}
Updates react-fast-compare to get support for Preact (559)