如何在React中设置文档标题? [英] How do you set the document title in React?
本文介绍了如何在React中设置文档标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为我的React应用程序设置文档标题(在浏览器标题栏中)。我尝试过使用 react-document-title (似乎已过期)并设置 document.title
在构造函数
和 componentDidMount()
- 没有这些解决方案有效。
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.
推荐答案
import React from 'react'
import ReactDOM from 'react-dom'
class Doc extends React.Component{
componentDidMount(){
document.title = "dfsdfsdfsd"
}
render(){
return(
<b> test </b>
)
}
}
ReactDOM.render(
<Doc />,
document.getElementById('container')
);
这对我有用。
编辑:如果您使用的是webpack-dev-server,则将内联设置为true
If you're using webpack-dev-server set inline to true
这篇关于如何在React中设置文档标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文