我想为我的 React 应用程序设置文档标题(在浏览器标题栏中)。我尝试过使用反应文档标题 https://github.com/gaearon/react-document-title(似乎已经过时)和设置document.title
in the constructor
and componentDidMount()
- 这些解决方案都不起作用。
对于 React 16.8+,您可以使用效果挂钩 https://reactjs.org/docs/hooks-effect.html在功能组件中:
import React, { useEffect } from 'react';
function Example() {
useEffect(() => {
document.title = 'My Page Title';
}, []);
}
管理所有有效的头标签,包括<title>
,以声明的方式,您可以使用反应头盔 https://github.com/nfl/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>
...
</>
)
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)