How to add SEO in react apps using Helmet

How to add SEO in react apps using Helmet

In this post we will give you information about How to add SEO in react apps using Helmet. Hear we will give you detail about How to add SEO in react apps using HelmetAnd how to use it also give you demo for it if it is necessary.

we will learn about seo in react apps by using the react helmet package.

In the single page apps, the SEO is the hard part to add because we are reusing single html page throughout the site.

There is a package called React helmet which helps us to control our head tags. React helmet provides us a Helmet component which takes the plain html meta tags and adds it inside the head tag to our pages.

Let’s see an example.

First, we need to install a react-helmet package from the npm package manager.

Run the below command in your terminal to install the react-helmet package.

npm i react-helmet

Consider our app has three routes like in the below image.

If we try to navigate to posts route or contact route we are seeing the same title and description tags for all routes.

Let’s use the Helmet component to control the head tags in our app.

import React from "react";import { Helmet } from "react-helmet";function App() {  return (    <div className="App">      <Helmet>        <title>My seo app</title>        <meta name="description" content="testing react helmet" />        <meta name="keywords" content="react,seo,helmet" />      </Helmet>      <h1>Hello react</h1>      <h2>This is app route</h2>    </div>  );}export default App;

In the above code, we first imported the Helmet component from the ‘react-helmet’ package then we passed the seo tags as children to the Helmet component.

output:

We successfully added the seo tags to our app.

Server-side rendering usage

React helmet package are also used in Server side rendering react apps.

Example for the server-side rendering.

import React from 'react';import { renderToString } from 'react-dom/server';import express from 'express';import App from './src/App';const app = express();app.get('/*', (req<span>, res) =&gt; {  const app = renderToString(&lt;App /&gt;);  const helmet = Helmet.renderStatic();const html = '    &lt;!doctype html&gt;    &lt;html <span>${helmet.htmlAttributes.toString()}&gt;        &lt;head&gt;            ${helmet.title.toString()}            ${helmet.meta.toString()}            ${helmet.link.toString()}        &lt;/head&gt;        &lt;body ${helmet.bodyAttributes.toString()}&gt;            &lt;div id="app"&gt;                ${app}            &lt;/div&gt;        &lt;/body&gt;    &lt;/html&gt;';  res.send(html);});app.listen(3000);

In the server side code, we first invoked the renderToString method by passing an App component, next we invoked the Helmet.renderStatic() to get the head data.

Each helmet property contains a toString() method which is used inside the html string.

Hope this code and post will helped you for implement How to add SEO in react apps using Helmet. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US