Sulav Jung Hamal - Blog - 2023/04/27
This article explains how to dynamically set the page title in React or Gatsby using the react-helmet library. By following these steps, you can set the page title dynamically based on the component's props, making it easier to manage and update page titles in your application.
react-helmet by running the following command in your terminal: //In terminal
npm install react-helmet
Helmet component from react-helmet in your component file:import { Helmet } from "react-helmet"
Helmet component in your component render method, and pass in the title as prop:render() {
return (
<div>
<Helmet>
<title>{this.props.title}</title>
</Helmet>
{/* rest of the component */}
</div>
);
}
title prop to the component:import React from "react"
import MyComponent from "../components/MyComponent"
const MyPage = () => {
const pageTitle = "My Page Title"
return <MyComponent title={pageTitle} />
}
export default MyPage
In this example, the title prop is passed to the MyComponent component, which is then used in the Helmet component to set the page title dynamically.
Note that the react-helmet library can be used to set other meta tags and attributes as well, such as the meta description, canonical URL, og:title, and more.
Using the Helmet component inside a template class can be helpful for setting dynamic page titles across multiple pages in a web application. By creating a template class that incorporates the Helmet component, you can easily include the necessary code to dynamically set the page title in each of your pages without having to repeat the same code in each page component. This approach can save time and reduce the likelihood of introducing errors or inconsistencies in your code. Additionally, if you need to update the page title across all pages, you can simply update the template class instead of having to modify each individual page component.
Explore & Blog
Thoughts, experiments, and insights from my journey building products and exploring technology.
Technology, Business, Retail
Sulav Jung Hamal ā 2026/03/12
Computer Science
Sulav Jung Hamal ā 2024/08/20