How to Put title and meta Inside Components in React 19
Reactmeta,Server-side rendering,title,TypeScript
In React 19, you can write <title> and <meta> directly in the JSX of a page component (a component). Wherever you write them, React moves them into the <head>, so you can output a per-page title and description without rewriting document.title in useEffect or adding a library. This works as a basic SEO setup for controlling how pages appear in search results.
In my test environment, I checked the contents of the <head> when switching pages, how the <title> in index.html is handled, what happens when two components both write a <title>, how an empty title appears, which tags are not moved to the <head>, and the output when rendering on the server.
The test environment was Ubuntu 24.04 and Node.js 22.22.0. I used react / react-dom 19.3.1, @types/react / @types/react-dom 19.3.0, typescript 7.0.2, esbuild 0.28.2, and jsdom 29.1.1. I type-checked the .tsx files with tsc, transpiled them with esbuild, and ran them on jsdom with the development build of React. The GIF was recorded by running the same components in Chromium with Playwright 1.56.1.
Writing title and meta inside a page component
According to the section on document metadata in the React 19 release post, until now you had to put <title>, <link>, and <meta> into <head> by hand inside an effect, or use a library such as react-helmet. From React 19, React moves these tags, when written inside a component, into the <head>.
The following Pages component switches between two pages with buttons. Each page component writes its own <title>, a description <meta>, and a <link rel="canonical"> that gives the canonical URL, all inside an <article>.
import { useState } from 'react';
// Each page component renders its own title, meta, and link
function HomePage() {
return (
<article>
<title>トップ | 猫の手ブログ</title>
<meta name="description" content="猫の手ブログのトップページです。" />
<link rel="canonical" href="https://example.com/" />
<h1>トップ</h1>
</article>
);
}
export function CatPage() {
return (
<article>
<title>猫の記事 | 猫の手ブログ</title>
<meta name="description" content="猫についての記事です。" />
<link rel="canonical" href="https://example.com/cats" />
<h1>猫の記事</h1>
</article>
);
}
export function Pages() {
const [page, setPage] = useState<'home' | 'cats' | 'none'>('home');
return (
<>
<nav>
<button onClick={() => setPage('home')}>トップ</button>
<button onClick={() => setPage('cats')}>猫の記事</button>
<button onClick={() => setPage('none')}>どちらも出さない</button>
</nav>
{page === 'home' && <HomePage />}
{page === 'cats' && <CatPage />}
</>
);
}
The index.html of Vite's React template has a <title> in <head> from the start. In this test as well, I rendered on an HTML page whose <head> contained <title>初期のタイトル</title>. The <head> and the contents of #root after the first render were as follows.
--- Initial render
document.title = "トップ | 猫の手ブログ"
head: <meta charset="utf-8"><title>トップ | 猫の手ブログ</title><title>初期のタイトル</title><meta name="description" content="猫の手ブログのトップページです。"><link rel="canonical" href="https://example.com/">
body: <nav><button>トップ</button><button>猫の記事</button><button>どちらも出さない</button></nav><article><h1>トップ</h1></article>
The three tags were removed from inside the <article> and placed in the <head>. The <title>初期のタイトル</title> that was originally in the <head> was not removed; React's <title> was inserted before it. According to the HTML spec's definition of document.title, document.title returns the text of the first <title> element in the document. That is why the title written by React is the one in effect.
Switching pages also swaps the head contents
When you switch pages with the buttons, the contents of the <head> are also swapped to match the page. The following GIF runs the same Pages in Chromium and records document.title and the contents of the <head> after each press.

The following shows the document.title and <head> after the first and second presses, from the jsdom run with the same sequence of presses.
--- After pressing "Cat article"
document.title = "猫の記事 | 猫の手ブログ"
head: <meta charset="utf-8"><title>猫の記事 | 猫の手ブログ</title><title>初期のタイトル</title><meta name="description" content="猫についての記事です。"><link rel="canonical" href="https://example.com/cats">
--- After pressing "Show neither"
document.title = "初期のタイトル"
head: <meta charset="utf-8"><title>初期のタイトル</title>
When the page component disappeared, the tags it had written were also removed from the <head>. When no component writes a <title>, the remaining 初期のタイトル returns as document.title. The same happened when I unmounted the entire app. The <title> in index.html works as a fallback while no React component writes a title.
When two components write a title, both end up in the head
I tested what happens when a shared layout and a page inside it both write a <title> and a description <meta>.
import type { ReactNode } from 'react';
// Both the shared layout and the page inside it render a title and description
function Layout({ children }: { children: ReactNode }) {
return (
<div className="layout">
<title>猫の手ブログ</title>
<meta name="description" content="サイト共通の説明です。" />
{children}
</div>
);
}
function CatPage() {
return (
<article>
<title>猫の記事 | 猫の手ブログ</title>
<meta name="description" content="猫についての記事です。" />
<h1>猫の記事</h1>
</article>
);
}
export function TwoTitles() {
return (
<Layout>
<CatPage />
</Layout>
);
}
--- Initial render
document.title = "猫の記事 | 猫の手ブログ"
head: <meta charset="utf-8"><title>猫の記事 | 猫の手ブログ</title><title>猫の手ブログ</title><title>初期のタイトル</title><meta name="description" content="サイト共通の説明です。"><meta name="description" content="猫についての記事です。">
React does not choose one and remove the other. There are three <title> elements and two description <meta> elements in the <head>. In this test, document.title became the page's title, but the note in the <title> reference says that how browsers and search engines behave when several components write a <title> at the same time is not defined. Do not rely on the order. Make sure only one component writes a <title> at any given time.
If you want to override the shared site title or description for each URL (each page), a library is a better fit. The supplementary note in the release post says that for simple cases, writing the tags is enough, but a library is stronger for things like overriding shared metadata based on the URL of the page currently open.
Using {} in a title makes it empty
If you mix a value such as a page number into a title and use JSX {} directly, the title ends up empty.
import { useState } from 'react';
// 🔴 Mixing a number in with {} means children is not a single string
export function TitleWithNumber() {
const [page, setPage] = useState(1);
return (
<>
<title>検索結果 {page}ページ目</title>
<button onClick={() => setPage(page + 1)}>次へ</button>
</>
);
}
--- Initial render
document.title = ""
head: <meta charset="utf-8"><title></title><title>初期のタイトル</title>
body: <button>次へ</button>
--- After pressing "Next"
document.title = ""
head: <meta charset="utf-8"><title></title><title>初期のタイトル</title>
body: <button>次へ</button>
According to the section on using variables in the title in the <title> reference, the children of <title> must be a single string. Writing 検索結果 {page}ページ目 makes the children an array that mixes strings and a number.
With browser rendering (react-dom/client), even the development build printed no warning, and the empty <title> was inserted into the <head>. Because it is placed before the title in index.html, document.title becomes empty.
When the same component was rendered on the server (renderToString), the development build printed the following error to the console. Since it is long, I show only the first two sentences.
[console.error] React expects the `children` prop of <title> tags to be a string, number, bigint, or object with a novel `toString` method but found an Array with length 3 instead. Browsers treat all child Nodes of <title> tags as Text content and React expects to be able to convert `children` of <title> tags to a single string value which is why Arrays of length greater than 1 are not supported.
When I searched the react-dom development build for files containing this message, the only match was the server-side file. For apps that do not render on the server, you cannot rely on this warning.
To fix it, pass a single string built with a template literal. The following code continues the same file as TitleWithNumber above, and useState is imported at the top of that file.
// Pass a single string built with a template literal
export function TitleWithTemplate() {
const [page, setPage] = useState(1);
return (
<>
<title>{`検索結果 ${page}ページ目`}</title>
<button onClick={() => setPage(page + 1)}>次へ</button>
</>
);
}
--- Initial render
document.title = "検索結果 1ページ目"
--- After pressing "Next"
document.title = "検索結果 2ページ目"
Things that are not moved to the head
The <title> reference and the <meta> reference list exceptions that are not moved to the <head>. A <title> inside an <svg> is a description of the graphic, not the document title, so it stays where it is. A <title> or <meta> with an itemProp also stays where it was written, because it describes part of the page rather than the whole document.
// Not moved to head: a title inside svg, and a meta with itemProp
export function Exceptions() {
return (
<section itemScope itemType="https://schema.org/Product">
<svg width="24" height="24" role="img">
<title>肉球のアイコン</title>
<circle cx="12" cy="12" r="10" />
</svg>
<meta itemProp="name" content="猫じゃらし" />
<h2>猫じゃらし</h2>
</section>
);
}
--- Initial render
document.title = "初期のタイトル"
head: <meta charset="utf-8"><title>初期のタイトル</title>
body: <section itemscope="" itemtype="https://schema.org/Product"><svg width="24" height="24" role="img"><title>肉球のアイコン</title><circle cx="12" cy="12" r="10"></circle></svg><meta itemprop="name" content="猫じゃらし"><h2>猫じゃらし</h2></section>
Both stayed inside the <section>, and the <head> did not change. Also, according to the <link> reference, a <link rel="stylesheet"> is moved to the <head> only when you give it a precedence, and a <link> with onLoad or onError is not moved. I did not test stylesheet handling in this article.
Output when rendering on the server
Even when rendering on the server, if you render the whole document including the <html> element with React, tags such as <title> written inside components end up in the <head>, just as they do in the browser. I rendered the following Document to HTML with renderToReadableStream from react-dom/server. The <head> contains only a charset <meta>.
import { CatPage } from './Pages';
// The whole document rendered on the server. The head contains only the charset
export function Document() {
return (
<html lang="ja">
<head>
<meta charSet="utf-8" />
</head>
<body>
<CatPage />
</body>
</html>
);
}
--- 1. Render the whole document from html with renderToReadableStream
<!DOCTYPE html><html lang="ja"><head><meta charSet="utf-8"/><title>猫の記事 | 猫の手ブログ</title><meta name="description" content="猫についての記事です。"/><link rel="canonical" href="https://example.com/cats"/></head><body><article><h1>猫の記事</h1></article></body></html>
The three tags written by CatPage inside <body> were output inside the <head>. The per-page title and description are included in the <head> of the HTML that the server returns.
When only CatPage, without an <html> element, was rendered with renderToString, the three tags were placed outside the <article> and came first in the output.
--- 2. Render only the page component with renderToString
<title>猫の記事 | 猫の手ブログ</title><meta name="description" content="猫についての記事です。"/><link rel="canonical" href="https://example.com/cats"/><article><h1>猫の記事</h1></article>
If you write <title>, <meta>, and <link> inside page components, React puts them into the <head>, both in the browser and when the whole document including the <html> element is rendered on the server. Only one component should write a <title> at a time, and its contents should be passed as a single string built with a template literal.
Posted by 86
Related Posts

How to Draw an Audio File’s Waveform in React
How to draw the waveform of an audio fil ...

How to Display Weekly Plans as a Timetable in React
Place meetings and study session events ...

How to Extract a React Custom Hook and Test It with renderHook
Steps for extracting a custom hook from ...

React Controlled vs Uncontrolled Inputs: Differences in Form Handling
A React input can be "controlled" by hol ...

How to Catch Errors with React Error Boundary
When an error is thrown during rendering ...
