Use Document Meta

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

import { useEffect } from "react";

export const useDocumentMeta = ({
  title,
  description,
}: {
  title: string;
  description?: string;
}) => {
  useEffect(() => {
    document.title = title;
    document
      .querySelector("meta[name=description]")
      ?.setAttribute("content", description ?? "");
  }, [title, description]);
};

Loved this post?

Comments

Interested in
working
with me?