Home
Courses
Blogs
Movies
More
HomeCoursesBlogsSearch-BlogShoppingEntertainmentBuild my SitePlaylistMusicReelsEarnYoutubeGithubLinkedinInstagramAPIContactAbout

Adding Social Media Share Buttons in React/Next.js using react-share

Jitender   ||    Jan 23, 2024   ||     3 min read

In the age of digital connectivity, sharing content on social media has become an integral part of online presence. Adding social media share buttons to your React or Next.js application can significantly enhance user engagement and expand your content's reach. In this tutorial, we'll explore how to integrate social media share buttons using the react-share library.

Getting Started

Before we begin, make sure you have a React or Next.js project set up. If you don't have one, you can create a new project using the following commands

For React

Copy ❐
npx create-react-app my-react-app
cd my-react-app

For Next.js

Copy ❐
npx create-next-app my-next-app
cd my-next-app

Now, let's install the react-share library using npm

Copy ❐
npm install react-share

Implementing Social Media Share Buttons

1. Import the necessary components

In your React or Next.js component file, import the required components from the react-share library.

Copy ❐
import { FacebookShareButton, TwitterShareButton, LinkedinShareButton } from 'react-share';
import { FaFacebook, FaTwitter, FaLinkedin } from 'react-icons/fa';

2. Create a Share component

Now, create a component that will render the social media share buttons.

Copy ❐
import React from 'react';

const ShareButtons = ({ url, title, description }) => {
  return (
    <div>
      <FacebookShareButton url={url} quote={title}>
        <FaFacebook size={32} round />
      </FacebookShareButton>

      <TwitterShareButton url={url} title={title}>
        <FaTwitter size={32} round />
      </TwitterShareButton>

      <LinkedinShareButton url={url} title={title} summary={description}>
        <FaLinkedin size={32} round />
      </LinkedinShareButton>
    </div>
  );
};

export default ShareButtons;

3. Use the Share component

Now, you can use the ShareButtons component wherever you want to display social media share buttons. Pass the url, title, and description as props to customize the content that will be shared.

Copy ❐
import React from 'react';
import ShareButtons from './ShareButtons';

const MyComponent = () => {
  const shareUrl = 'https://your-website.com';
  const shareTitle = 'Check out this amazing content!';
  const shareDescription = 'Learn how to add social media share buttons in React/Next.js using react-share. #React #NextJS #SocialMedia';

  return (
    <div>
      {/* Your component content here */}
      <ShareButtons url={shareUrl} title={shareTitle} description={shareDescription} />
    </div>
  );
};

export default MyComponent;

Conclusion

Integrating social media share buttons in your React or Next.js application is a straightforward process with the react-share library. Encouraging users to share your content on platforms like Facebook, Twitter, and LinkedIn can help increase visibility and drive more traffic to your website. Experiment with different styles and configurations to match the overall design of your application and encourage social sharing. Happy coding!

Thank you ...