The Share API in React Native allows apps to open the native sharing dialog so users can share text, links, or other content using installed apps like WhatsApp, Email, or SMS.
Share module from React NativeThe Share API exposes a single main method called Share.share(). It accepts an object containing data to be shared and returns a Promise with the result.
// Import Share module from react-native
import { Share } from "react-native";
// Function to trigger share dialog
Share.share({
message: "Hello from React Native!",
url: "https://reactnative.dev",
title: "React Native"
});
// Complete example using a button
import React from "react";
import { View, Button, Share } from "react-native";
export default function App() {
const onShare = async () => {
try {
await Share.share({
message: "Check out React Native Share API"
});
} catch (error) {
console.log(error.message);
}
};
return (
<View>
<Button title="Share" onPress={onShare} />
</View>
);
}
When the Share button is pressed, the native share dialog opens, allowing the user to select an app to share the message.
Since we can't run actual React Native code here in the browser, below is a Simulation of how the Native Share Sheet behaves. Use the phone mockup below to understand the flow.
Article: "Learning React Native"
Tap share to test