Sharing lets users send text or files from your app to other apps. Clipboard lets users copy and paste text easily.
When you want users to share a message or link with friends via other apps.
When users need to copy text from your app to paste somewhere else.
When you want to let users paste text into your app from other apps.
When you want to share images or files from your app to social media or email.
When you want to provide a quick way to copy a code or URL for later use.
Syntax
React Native
import { Share } from'react-native';
import Clipboard from'@react-native-clipboard/clipboard';
// To share text
Share.share({ message: 'Hello from my app!' });
// To copy text to clipboard
Clipboard.setString('Text to copy');
// To get text from clipboard
const text = await Clipboard.getString();
Use Share.share() to open the system share dialog.
Use Clipboard.setString() to copy text, and Clipboard.getString() to read text.
Examples
This opens the share dialog with the message to share.
React Native
Share.share({ message: 'Check out this cool app!' });
This copies the text so the user can paste it elsewhere.
React Native
Clipboard.setString('Copy this text');
This reads the current text from the clipboard and logs it.
5. You want to create a button that copies user input text to clipboard and then shares it. Which code snippet correctly combines Clipboard and Share APIs?
hard
A. const copyAndShare = async (text) => {
await Clipboard.setString(text);
await Share.share({ message: text });
};
B. const copyAndShare = (text) => {
Clipboard.setString(text);
Share.share(text);
};
D. const copyAndShare = async (text) => {
await Clipboard.getString();
await Share.share({ message: text });
};
Solution
Step 1: Use async/await for Clipboard.setString
Clipboard.setString returns a promise, so await ensures text is copied before sharing.
Step 2: Pass correct object to Share.share
Share.share requires an object with message property containing the text to share.
Step 3: Verify combined usage
const copyAndShare = async (text) => {
await Clipboard.setString(text);
await Share.share({ message: text });
}; correctly awaits Clipboard.setString and then shares the same text properly.
Final Answer:
const copyAndShare = async (text) => {
await Clipboard.setString(text);
await Share.share({ message: text });
}; -> Option A