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

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.

主站蜘蛛池模板: 阜新市| 沙坪坝区| 贡山| 张家港市| 循化| 乌鲁木齐县| 金秀| 瑞丽市| 光泽县| 蓝田县| 肥城市| 密云县| 宁阳县| 通城县| 砚山县| 鸡泽县| 虞城县| 岑巩县| 襄汾县| 株洲市| 永宁县| 蓬安县| 桃园县| 涞水县| 宜春市| 隆回县| 赣州市| 定远县| 奉新县| 金山区| 军事| 阿克| 惠水县| 弥渡县| 垫江县| 郓城县| 顺平县| 榆中县| 高台县| 全州县| 义乌市|