0
0
React Nativemobile~20 mins

Sharing and clipboard in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sharing and Clipboard Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when this React Native clipboard code runs?
Consider this React Native code snippet that copies text to the clipboard and then reads it back to display in an alert. What will the alert show?
React Native
import Clipboard from '@react-native-clipboard/clipboard';
import {Alert} from 'react-native';

Clipboard.setString('Hello World');
Clipboard.getString().then(text => Alert.alert('Clipboard content', text));
AAn alert with title 'Clipboard content' and message 'Hello World'
BAn alert with title 'Clipboard content' and an empty message
CNo alert appears because getString returns undefined
DA runtime error because Clipboard methods are not asynchronous
Attempts:
2 left
💡 Hint
Clipboard.setString stores text, getString returns a Promise with that text.
🧠 Conceptual
intermediate
1:30remaining
Which React Native API is best for sharing text to other apps?
You want to let users share a text message from your app to other apps like email or messaging. Which React Native API should you use?
AAnimated API to animate the text before sharing
BLinking API to open a URL with the text
CClipboard API to copy text and ask user to paste
DShare API to open the native share dialog
Attempts:
2 left
💡 Hint
Sharing means sending data to other apps, not just copying.
lifecycle
advanced
2:00remaining
What is the effect of calling Clipboard.getString() inside useEffect without dependencies?
In a React Native functional component, what happens if you call Clipboard.getString() inside useEffect with no dependency array?
React Native
import React, {useEffect} from 'react';
import Clipboard from '@react-native-clipboard/clipboard';

function MyComponent() {
  useEffect(() => {
    Clipboard.getString().then(text => console.log(text));
  });
  return null;
}
AClipboard.getString() runs on every render causing repeated logs
BClipboard.getString() runs once when component mounts
CClipboard.getString() never runs because useEffect needs dependencies
DClipboard.getString() causes a runtime error without dependencies
Attempts:
2 left
💡 Hint
useEffect without [] runs after every render.
🔧 Debug
advanced
2:00remaining
Why does this sharing code fail to open the share dialog?
This React Native code tries to share text but nothing happens. What is the likely cause?
React Native
import {Share} from 'react-native';

async function shareText() {
  try {
    await Share.share({message: 'Hello!'});
  } catch (error) {
    console.error(error);
  }
}
Amessage property should be called text, not message
BMissing await before Share.share causes the dialog not to open
CShare API requires a URL, not plain text
DTry-catch block prevents the share dialog from showing
Attempts:
2 left
💡 Hint
Share.share returns a Promise that should be awaited.
📝 Syntax
expert
1:30remaining
What error does this clipboard code produce?
What error will this React Native clipboard code cause?
React Native
import Clipboard from '@react-native-clipboard/clipboard';

Clipboard.setString('Test');
Clipboard.getString().then(text => console.log(text));
AReferenceError because Clipboard is not imported correctly
BSyntaxError due to missing semicolon after setString
CTypeError because getString does not take a callback
DNo error, logs 'Test' correctly
Attempts:
2 left
💡 Hint
Check the signature of Clipboard.getString() in React Native.