Skip to content

useTitle

React hook to change the page title.

Add the hook via the CLI:

sh
npx @novajslabs/cli add useTitle
sh
npx @novajslabs/cli add useTitle
sh
pnpm dlx @novajslabs/cli add useTitle

Or copy and paste the code into your project:

ts
import { useState } from "react";

export const useTitle = () => {
  const [title, setTitle] = useState<string>(document.title);

  const changeTitle = (newTitle: string) => {
    document.title = newTitle;
    setTitle(newTitle);
  };

  return { title, changeTitle };
};
js
import { useState } from "react";

export const useTitle = () => {
  const [title, setTitle] = useState(document.title);

  const changeTitle = (newTitle) => {
    document.title = newTitle;
    setTitle(newTitle);
  };

  return { title, changeTitle };
};

Requirements

React 16.8 or higher

Return values

title

Type: string

The current value of the page title.

changeTitle

Type: function

Set the page title. This function accepts the new title.

Examples of common use cases