Bird
Raised Fist0
React Nativemobile~20 mins

Sharing and clipboard in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Share and Clipboard Demo
This screen lets users enter text, copy it to the clipboard, and share it using the device's share options.
Target UI
-------------------------
| Share and Clipboard    |
|-----------------------|
| [ Text Input          ]|
|                       |
| [ Copy to Clipboard ]  |
| [ Share Text        ]  |
-------------------------
A TextInput for user to type text
A button labeled 'Copy to Clipboard' that copies the input text to clipboard
A button labeled 'Share Text' that opens the device share dialog with the input text
Show a simple alert or message confirming the text was copied
Starter Code
React Native
import React, { useState } from 'react';
import { View, TextInput, Button, Alert, StyleSheet } from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard';
import Share from 'react-native-share';

export default function ShareClipboardScreen() {
  const [text, setText] = useState('');

  // TODO: Add copyToClipboard function

  // TODO: Add shareText function

  return (
    <View style={styles.container}>
      <TextInput
        style={styles.input}
        placeholder="Type something here"
        value={text}
        onChangeText={setText}
        accessibilityLabel="Text input for sharing and clipboard"
      />
      <Button title="Copy to Clipboard" onPress={() => {}} />
      <Button title="Share Text" onPress={() => {}} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 16,
    justifyContent: 'center',
    backgroundColor: '#fff'
  },
  input: {
    borderColor: '#888',
    borderWidth: 1,
    padding: 12,
    marginBottom: 12,
    borderRadius: 4
  }
});
Task 1
Task 2
Task 3
Task 4
Solution
React Native
import React, { useState } from 'react';
import { View, TextInput, Button, Alert, StyleSheet } from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard';
import Share from 'react-native-share';

export default function ShareClipboardScreen() {
  const [text, setText] = useState('');

  const copyToClipboard = () => {
    Clipboard.setString(text);
    Alert.alert('Copied!', 'Text copied to clipboard.');
  };

  const shareText = async () => {
    try {
      await Share.open({
        message: text
      });
    } catch (error) {
      // User cancelled or error
    }
  };

  return (
    <View style={styles.container}>
      <TextInput
        style={styles.input}
        placeholder="Type something here"
        value={text}
        onChangeText={setText}
        accessibilityLabel="Text input for sharing and clipboard"
      />
      <Button title="Copy to Clipboard" onPress={copyToClipboard} />
      <Button title="Share Text" onPress={shareText} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 16,
    justifyContent: 'center',
    backgroundColor: '#fff'
  },
  input: {
    borderColor: '#888',
    borderWidth: 1,
    padding: 12,
    marginBottom: 12,
    borderRadius: 4
  }
});

We use React Native's useState to keep track of the text input by the user. The copyToClipboard function uses the Clipboard.setString method to copy the current text to the device clipboard and then shows a simple alert to confirm the action.

The shareText function uses the react-native-share library to open the device's native share dialog with the text message. We handle errors silently, such as when the user cancels sharing.

Both functions are connected to their respective buttons' onPress handlers. The UI is simple and accessible, with labels and clear buttons.

Final Result
Completed Screen
-------------------------
| Share and Clipboard    |
|-----------------------|
| [ Type something here ]|
|                       |
| [ Copy to Clipboard ]  |
| [ Share Text        ]  |
-------------------------
User types text in the input field
Tapping 'Copy to Clipboard' copies the text and shows an alert 'Copied! Text copied to clipboard.'
Tapping 'Share Text' opens the device's share dialog with the typed text
Stretch Goal
Add a button that pastes text from the clipboard into the input field
💡 Hint
Use Clipboard.getString() to read clipboard content and update the text state

Practice

(1/5)
1. What is the main purpose of the Clipboard API in React Native?
easy
A. To copy and paste text between apps
B. To share images on social media
C. To send emails from the app
D. To open external web links

Solution

  1. Step 1: Understand Clipboard functionality

    The Clipboard API is designed to handle text copying and pasting between apps.
  2. Step 2: Compare with other options

    Sharing images, sending emails, and opening links are not Clipboard's main tasks.
  3. Final Answer:

    To copy and paste text between apps -> Option A
  4. Quick Check:

    Clipboard = copy-paste text [OK]
Hint: Clipboard is for copy-paste text only [OK]
Common Mistakes:
  • Confusing Clipboard with sharing images
  • Thinking Clipboard sends emails
  • Assuming Clipboard opens web links
2. Which of the following is the correct way to import the Clipboard API in React Native?
easy
A. import { Clipboard } from 'react-native';
B. import Clipboard from '@react-native-clipboard/clipboard';
C. import Clipboard from 'react-native';
D. import Clipboard from 'react-native-clipboard';

Solution

  1. Step 1: Identify the official Clipboard package

    React Native moved Clipboard to a separate package '@react-native-clipboard/clipboard'.
  2. Step 2: Check import syntax

    The correct import is default import from '@react-native-clipboard/clipboard'.
  3. Final Answer:

    import Clipboard from '@react-native-clipboard/clipboard'; -> Option B
  4. Quick Check:

    Clipboard import = '@react-native-clipboard/clipboard' [OK]
Hint: Clipboard is from '@react-native-clipboard/clipboard' package [OK]
Common Mistakes:
  • Importing Clipboard directly from 'react-native'
  • Using named import instead of default
  • Using wrong package names
3. What will be the output of this React Native code snippet?
import Clipboard from '@react-native-clipboard/clipboard';

Clipboard.setString('Hello World');
const text = await Clipboard.getString();
console.log(text);
medium
A. undefined
B. Error: Clipboard.getString is not a function
C. 'Hello World'
D. null

Solution

  1. Step 1: Understand Clipboard.setString usage

    This sets the clipboard content to 'Hello World'.
  2. Step 2: Understand Clipboard.getString usage

    It asynchronously retrieves the clipboard content, which is 'Hello World'.
  3. Final Answer:

    'Hello World' -> Option C
  4. Quick Check:

    Clipboard content = 'Hello World' [OK]
Hint: Clipboard.getString returns the last set string [OK]
Common Mistakes:
  • Forgetting await on Clipboard.getString
  • Expecting undefined or null instead of set string
  • Confusing Clipboard API methods
4. You wrote this code to share text but it does not work:
import { Share } from 'react-native';

const shareText = async () => {
  Share.share('Hello!');
};
What is the error?
medium
A. You cannot share text in React Native
B. Share must be imported from '@react-native-share/share'
C. Share.share is not async function
D. Share.share requires an object with a message property

Solution

  1. Step 1: Check Share.share argument type

    Share.share expects an object like { message: 'text' }, not a string.
  2. Step 2: Identify correct usage

    Passing a string directly causes failure; must pass { message: 'Hello!' }.
  3. Final Answer:

    Share.share requires an object with a message property -> Option D
  4. Quick Check:

    Share.share argument = object with message [OK]
Hint: Share.share needs { message: 'text' } object [OK]
Common Mistakes:
  • Passing string directly instead of object
  • Importing Share from wrong package
  • Ignoring async/await usage
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); };
C. const copyAndShare = async (text) => { Clipboard.setString(text); Share.share({ message: await Clipboard.getString() }); };
D. const copyAndShare = async (text) => { await Clipboard.getString(); await Share.share({ message: text }); };

Solution

  1. Step 1: Use async/await for Clipboard.setString

    Clipboard.setString returns a promise, so await ensures text is copied before sharing.
  2. Step 2: Pass correct object to Share.share

    Share.share requires an object with message property containing the text to share.
  3. 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.
  4. Final Answer:

    const copyAndShare = async (text) => { await Clipboard.setString(text); await Share.share({ message: text }); }; -> Option A
  5. Quick Check:

    Await Clipboard.setString + Share.share({ message }) [OK]
Hint: Await Clipboard.setString before Share.share({ message }) [OK]
Common Mistakes:
  • Not awaiting Clipboard.setString
  • Passing string directly to Share.share
  • Calling Clipboard.getString unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard