How to Integrate OpenGraph in React to Boost SEO and Social Sharing

esaraviam

Luis Esteban Saravia M

Posted on September 23, 2024

How to Integrate OpenGraph in React to Boost SEO and Social Sharing

In the world of web development, ensuring that your pages look great when shared on social media is crucial for the success of your projects. In this post, I’ll show you how to add OpenGraph metadata to your React application to optimize how your pages or apps are displayed when shared.

What is OpenGraph?

OpenGraph is a protocol that allows developers to define how content should appear when shared on social media platforms like Facebook, Twitter, and LinkedIn. By using OpenGraph, you can control how key elements like the title, description, and preview image appear, enhancing both the appearance and SEO of your content on social media.

Getting Started: Initial Setup in React

To manage metadata in React, we’ll use a package called react-helmet, which allows us to easily manipulate the content inside the

of our documents.

1. Install react-helmet

The first thing we need to do is install the react-helmet package in our React project. Open your terminal and run the following command:

npm install react-helmet

2. Create the OpenGraphMeta Component

Next, we’ll create a reusable React component that will handle the OpenGraph metadata. This component can be easily configured and used across different pages in your application.

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

type OpenGraphMetaProps = {
  title: string;
  description: string;
  url: string;
  image: string;
  siteName?: string;
};

const OpenGraphMeta: React.FC<OpenGraphMetaProps> = ({ title, description, url, image, siteName }) => {
  return (
    <Helmet>
      {/* OpenGraph metadata */}
      <meta property="og:title" content={title} />
      <meta property="og:description" content={description} />
      <meta property="og:url" content={url} />
      <meta property="og:image" content={image} />
      <meta property="og:type" content="website" />
      {siteName && <meta property="og:site_name" content={siteName} />}

      {/* Twitter metadata */}
      <meta name="twitter:card" content="summary_large_image" />
      <meta name="twitter:title" content={title} />
      <meta name="twitter:description" content={description} />
      <meta name="twitter:image" content={image} />
    </Helmet>
  );
};

export default OpenGraphMeta;
Enter fullscreen mode Exit fullscreen mode

3. Using the Component in a Page

Now that we have our OpenGraphMeta component, let’s see how we can use it inside a page in our React app.

import React from 'react';
import OpenGraphMeta from './OpenGraphMeta';

const MyPage: React.FC = () => {
  return (
    <div>
      {/* Add OpenGraph metadata */}
      <OpenGraphMeta
        title="My Awesome Page"
        description="This is the description of my page."
        url="https://www.mypage.com"
        image="https://www.mypage.com/image.jpg"
        siteName="My Website"
      />

      {/* Page content */}
      <h1>Welcome to My Page!</h1>
      <p>This is the content of the page you're visiting.</p>
    </div>
  );
};

export default MyPage;
Enter fullscreen mode Exit fullscreen mode

Conclusion

Integrating OpenGraph in React is a great way to improve the visibility and presentation of your pages when shared on social media. By using a dynamic component like OpenGraphMeta, you can easily manage these metadata and optimize your content for different platforms.

Now that you have this implementation ready, it’s time to put it into practice! Have you used OpenGraph before? Share your experiences in the comments below.

This post is designed to be clear, technical, and easy to follow, perfect for a developer audience on Medium. Let me know if you’d like to make any changes or additions!

💖 💪 🙅 🚩
esaraviam
Luis Esteban Saravia M

Posted on September 23, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related