How to add Web Share in Next.js ?
Last Updated :
25 Jul, 2024
The Web Share API enables web applications to share content (like URLs, text, or files) to other apps installed on a user's device, such as social media platforms, messaging apps, or email clients. Integrating the Web Share API into a Next.js project enhances user experience by providing a seamless way to share content directly from the web application.
In this article, we are going to learn how we can add web share in NextJS.
Approach
To add our web share we are going to use the react-web-share package. The react-web-share package helps us to integrate the web share in our app. So first, we will install the react-web-share package and then we will add a web share on our homepage.
Steps to Add Web Share in Next.js App
Step 1: Create a React application using the following command.
npx create-react-app gfg
Step 2: After creating your project folder i.e. gfg, move to it using the following command.
cd gfg
Step 3: Now we will install the react-web-share package using the below command
npm i react-web-share
Project Structure:

The updated dependencies in the package.json file are:
"dependencies": {
"next": "14.2.4",
"react": "^18",
"react-dom": "^18",
"react-web-share": "^2.0.2"
}
Example: This example demonstrates adding the webshare in next js application with the help of react-web-share npm package.
index.js
import React from "react";
import { RWebShare } from "react-web-share";
export default function WebShareGfg() {
return (
<div>
<h1>NextJs Web Share - GeeksforGeeks</h1>
<RWebShare
data={{
text: "Web Share - GfG",
url: "http://localhost:3000",
title: "GfG",
}}
onClick={() => console.log("shared successfully!")}
>
<button>Share on Web</button>
</RWebShare>
</div>
);
};
Explanation: In the above example first, we are importing the RWebShare component and after that, we are using the component to add the web share in our app.
Steps to run the application: Run the below command in the terminal to run the app.
npm run dev
Output:
Similar Reads
How to Add Tweets in Next.js ? Adding tweets in Next.js involves using Twitter's embed feature or API to display tweets. You can embed tweets directly in components or fetch and display them using server-side rendering or client-side data fetching. In this article, we are going to learn how we can add Tweets in NextJs.ApproachTo
2 min read
How to add Video Player in Next.js ? Adding a video player in a Next.js application enhances the user experience by enabling video playback. The react-player library simplifies integrating various video sources such as YouTube, Vimeo, and local files. In this article, we are going to learn how we can add Video Player in NextJS. Approac
2 min read
How to Use the App Directory in Next.js? The App Directory in Next.js provides a powerful way to structure and manage your application's pages and components. It simplifies the routing and organization of your project, enabling you to build complex web applications with ease.Understanding the App DirectoryThe App Directory is a new structu
3 min read
How to add ESLint in Next.js ? ESLint is a popular linting tool for identifying and fixing issues in JavaScript code. Adding ESLint to your Next.js project ensures that your code follows best practices and maintains a consistent style. This enhances code quality, helps catch errors early, and makes the codebase easier to maintain
3 min read
How to Get URL pathname in Next.js? Next.js is a powerful React framework that simplifies the process of building server-rendered and statically generated web applications. One common requirement in web development is to get the URL pathname, which can be useful for various purposes such as conditional rendering, routing, and analytic
3 min read