官术网_书友最值得收藏!

Controlling document heads with React Helmet

When developing a web application, it is crucial that you can control your document heads. You might want to change the title or description, based on the content you are presenting.

React Helmet is a great package that offers this on the fly, including overriding multiple headers and server-side rendering.

Install it with the following command:

npm install --save react-helmet

You can add all standard HTML headers with React Helmet.

I recommend keeping standard head tags inside your template. They have the advantage that, before React has rendered, there is always the default document head. For our case, you can directly apply a title and description in App.js.

Import react-helmet at the top of the file:

import { Helmet } from 'react-helmet';

Add Helmet itself directly above postForm div:

<Helmet>
<title>Graphbook - Feed</title>
<meta name="description" content="Newsfeed of all your friends on
Graphbook" />
</Helmet>

If you reload the browser and watch the title on the tab bar of your browser carefully, you will see that it changes from Graphbook to Graphbook - Feed. This behavior happens because we already defined a title inside index.html. When React finishes rendering, the new document head is applied.

主站蜘蛛池模板: 察隅县| 富阳市| 正阳县| 疏勒县| 连平县| 鲁甸县| 确山县| 淮北市| 塘沽区| 澄江县| 益阳市| 光泽县| 娄烦县| 重庆市| 开封市| 榆社县| 含山县| 江门市| 威信县| 华阴市| 休宁县| 万宁市| 丹凤县| 阿克苏市| 洞口县| 金昌市| 龙胜| 曲周县| 西青区| 崇礼县| 泽州县| 敖汉旗| 金山区| 建瓯市| 青冈县| 乌海市| 临潭县| 武川县| 萍乡市| 永春县| 建昌县|