You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionApp(){// Use "My Web App" if no child overrides thisreturn(<DocumentTitletitle='My Web App'><SomeRouter/></DocumentTitle>);}functionHomePage(){// Use "Home" while this component is mountedreturn(<DocumentTitletitle='Home'><h1>Home, sweet home.</h1></DocumentTitle>);}classNewArticlePageextendsReact.Component{constructor(props){super(props);this.state={title: 'Untitled'};}render(){// Update using value from state while this component is mountedreturn(<DocumentTitletitle={this.state.title}><div><h1>New Article</h1><inputvalue={this.state.title}onChange={(e)=>this.setState({title: e.target.value})}/></div></DocumentTitle>);}}
Server Usage
If you use it on server, call DocumentTitle.rewind()after rendering components to string to retrieve the title given to the innermost DocumentTitle. You can then embed this title into HTML page template.
Because this component keeps track of mounted instances, you have to make sure to call rewind on server, or you'll get a memory leak.
But What About Meta Tags?
Looking for something more powerful? Check out React Helmet!
About
Declarative, nested, stateful, isomorphic document.title for React