Search code examples
reactjstypescriptcreate-react-app

React TypeScript 16.8 - How to change the title of the page without Helmet


I've made a 404 not found page, but when I browse to it I want the title of the page to change, I dont want to use Helmet but I cant seem to get constructor or componentDidMount() to work, Im using the React 16.8.6 and Create React App Typescript was my starting point.

import React from 'react';
import logo from '../images/logo.svg';

const NotFound: React.FC = () => {

  return (
    <div className="home-grid">
      <header className="center-logo">
          <img src={logo} alt="My Logo" />
      </header>
      <footer className="home-footer">
        Error 404 page not found
      </footer>
    </div>

  );
}

export default NotFound;

Solution

  • you can just set the title of the page with regular javascript.

    document.title = '...'

    componentDidMount can only be used on classes, for function components use (useEffect) hooks instead.

      useEffect(() => {
        document.title = 'something...';
      }, [])