Use Document Meta

A hook to dynamically change document's meta title & description.

tsx
1import { useEffect } from "react";
2
3export const useDocumentMeta = ({
4  title,
5  description,
6}: {
7  title: string;
8  description?: string;
9}) => {
10  useEffect(() => {
11    document.title = title;
12    document
13      .querySelector("meta[name=description]")
14      ?.setAttribute("content", description ?? "");
15  }, [title, description]);
16};

Loved this post?

Comments

Interested in
working
with me?

Let's Connect

Stay in touch 👇

© 2021 - 2025 itsrakesh. v2.