How do you set the document title in React?
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'));
This works for me.
Edit: If you're using webpack-dev-server set inline to true
You can use 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> ... </> ) }}
For React 16.8, you can do this with a functional component using useEffect.
For Example:
useEffect(() => { document.title = "new title"}, []);
Having the second argument as an array calls useEffect only once, making it similar to componentDidMount
.