Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
1 min readReact

Render document metadata from React components

Set a route’s title and description next to its data with React 19 document metadata.

You navigate from the catalog to a product, but the tab still says Catalog. The app shell owns the title and the product route owns the product name. With React 19 you can put the title next to the route that has the data.

Render one title with the active route

React moves document metadata into the head. Return <title> and a description <meta> beside the route content. In this small catalog, React replaces the active screen as you change the selected product.

import { useState } from 'react'

const products = {
  mug: { name: 'Trail mug', description: 'A steel mug with a folding handle.' },
  stove: { name: 'Camp stove', description: 'A small stove for one pot.' },
}

function ProductPage({ product }) {
  const title = `${product.name} | Camp Store`
  return (
    <>
      <title>{title}</title>
      <meta name="description" content={product.description} />
      <h1>{product.name}</h1>
    </>
  )
}

export default function Catalog() {
  const [selected, setSelected] = useState('mug')
  return (
    <>
      <nav aria-label="Products">
        <button type="button" onClick={() => setSelected('mug')}>Mug</button>
        <button type="button" onClick={() => setSelected('stove')}>Stove</button>
      </nav>
      <main><ProductPage product={products[selected]} /></main>
    </>
  )
}

Select Stove and the tab reads Camp stove | Camp Store. The description tag in the head now describes the stove.

Build the title string before you pass it to <title> so React gets one text child. The heading and title both read the current product's name.

Remove the competing default

React hoists the tags. If the shell still renders its Catalog title alongside the product title, you have two title elements. Move the default to the route that needs it, or let the shell supply a fallback only when no page title exists.

Inspect the document head after clicking between routes. If your app renders on the server, check the HTML from a direct request too. Look for one title and one description in each, including any tags your framework adds.