TanStackT
TanStack6mo ago
1 reply
comprehensive-tomato

How to Dynamically Update the Page Title After Rendering

How can I dynamically update the <title> tag after the page has been rendered?

I am currently using the following code to render the title for pages:

head: ({ loaderData }) => {
  const meta: AnyRouteMatch["meta"] = [];
  if (loaderData?.title) {
    meta.push({ title: titleTag(loaderData.title) });
  }
  return {
    meta,
  };
},

In my app, there is a use case where I receive the title from WebSocket messages.

I tried updating the title with document.title = event.data.title, but the problem is that when I navigate to the home page (client side), the dynamically set title still shows and does not update correctly.
Was this page helpful?