Skip to content

Instantly share code, notes, and snippets.

@apexskier
Last active June 29, 2021 14:19
Show Gist options
  • Save apexskier/a27daa87798c77c89e4fdfa50904ca57 to your computer and use it in GitHub Desktop.
Save apexskier/a27daa87798c77c89e4fdfa50904ca57 to your computer and use it in GitHub Desktop.
useDocumentTitle template literal hook
import React from "react";
export function useDocumentTitle(
strings: TemplateStringsArray,
...args: ReadonlyArray<string>
): void {
React.useEffect(() => {
let title = "";
strings.forEach((part, i) => {
title += part;
if (args[i]) {
title += args[i];
}
});
document.title = title;
}, args);
}
/*
useDocumentTitle`App | ${variable}`
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment