【问题标题】:React TypeScript 16.8 - How to change the title of the page without HelmetReact TypeScript 16.8 - 如何在没有 Helmet 的情况下更改页面标题
【发布时间】:2019-07-19 12:27:49
【问题描述】:

我创建了一个 404 not found 页面,但是当我浏览到它时,我希望页面的标题发生变化,我不想使用 Helmet 但我似乎无法让构造函数或 componentDidMount() 工作,我使用 React 16.8.6 和 Create React App Typescript 是我的出发点。

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;

【问题讨论】:

    标签: reactjs typescript create-react-app


    【解决方案1】:

    您可以使用常规 javascript 设置页面的标题。

    document.title = '...'

    componentDidMount 只能在类上使用,对于函数组件,请改用 (useEffect) 钩子。

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

    【讨论】:

    • Flo 感谢代码,当我添加它时,我得到一个打字稿错误,Cannot find name 'useEffect'.ts(2304) 我需要安装和导入 useEffect 吗??
    • import { useEffect } from 'react'
    猜你喜欢
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 2010-12-07
    相关资源
    最近更新 更多