DEV Community

Cover image for Minimal SEO in React JS - React Helmet
Neeraj Singh
Neeraj Singh

Posted on

Minimal SEO in React JS - React Helmet

Using react-helmet

1. Install the library:

npm install react-helmet
Enter fullscreen mode Exit fullscreen mode

2. Update your component to use react-helmet:

import React from 'react';
import { Helmet } from 'react-helmet';

const App = () => {
    const appName = "Your Application Name";
    const dynamicPageTitle = `Welcome to ${appName}`;

    return (
        <div>
            <Helmet>
                <title>{dynamicPageTitle}</title>
            </Helmet>
            <h1>Welcome to {appName}</h1>
        </div>
    );
};

export default App;

Enter fullscreen mode Exit fullscreen mode

3. Result:

The page title will dynamically update, and search engines will be able to pick it up when indexing.
Using JavaScript (document.title)
You can also directly set the title in the useEffect hook:

import React, { useEffect } from 'react';

const App = () => {
    const appName = "Your Application Name";

    useEffect(() => {
        document.title = `Welcome to ${appName}`;
    }, [appName]);

    return (
        <div>
            <h1>Welcome to {appName}</h1>
        </div>
    );
};

export default App;
Enter fullscreen mode Exit fullscreen mode

Best Practices for SEO

  1. Unique Titles: Ensure each page in your app has a unique and descriptive title.

  2. Server-Side Rendering (SSR): If your app needs better SEO, consider using frameworks like Next.js, which support server-side rendering.

  3. Meta Tags: Along with the title, update meta tags using react-helmet or other similar methods to include descriptions, keywords, and canonical URLs.

<Helmet>
    <title>{dynamicPageTitle}</title>
    <meta name="description" content="Description of the application or page" />
    <meta name="keywords" content="your, keywords, here" />
</Helmet>

Enter fullscreen mode Exit fullscreen mode

These steps ensure your React app's title is dynamically updated and optimized for search engines like Google.

Top comments (3)

Collapse
 
gleamso profile image
gleamso

Nice tutorial!
Regarding meta tags, you can validate them using one of the following tools/services

  1. opengraph.xyz/
  2. gleam.so/tools/meta-validator (I made it)

Don't forget to generate the OpenGraph image ;)
Try my tool: gleam.so

Collapse
 
neerajs profile image
Neeraj Singh

Thanks for additional input. I will try the tool

Collapse
 
ciphernutz profile image
Ciphernutz

Great post! React Helmet is a handy tool for improving SEO in React apps.