你如何在 React 中设置文档标题? [英] How do you set the document title in React?

查看:29
本文介绍了你如何在 React 中设置文档标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的 React 应用程序设置文档标题(在浏览器标题栏中).我曾尝试使用 react-document-title(似乎已过时)并设置 constructor 和 componentDidMount() 中的 >document.title - 这些解决方案都不起作用.

I would like to set the document title (in the browser title bar) for my React application. I have tried using react-document-title (seems out of date) and setting document.title in the constructor and componentDidMount() - none of these solutions work.

推荐答案

您可以使用 React Helmet:

import React from 'react'
import { Helmet } from 'react-helmet'

const TITLE = 'My Page Title'

class MyComponent extends React.PureComponent {
  render () {
    return (
      <>
        <Helmet>
          <title>{ TITLE }</title>
        </Helmet>
        ...
      </>
    )
  }
}

这篇关于你如何在 React 中设置文档标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆