easyReact Native#35

Clipboard copy/paste

Prompt

Use expo-clipboard to copy a text string to the clipboard, then paste it back and display the pasted text.

Solution

const [pasted, setPasted] = useState('')
const handleCopy = async () => {
  await Clipboard.setStringAsync('Hello from clipboard!')
  const text = await Clipboard.getStringAsync()
  setPasted(text)
}
return (
  <View>
    <Button title="Copy & Paste" onPress={handleCopy} />
    <Text>{pasted}</Text>
  </View>
)
Mentor's take

The clipboard looks like the most trivial API in the SDK, and that's exactly why it's a good probe: the correct answer is mostly about what the clipboard is — a system-wide, OS-owned buffer shared by every app on the device — and what follows from that.

Mechanics first: expo-clipboard is the maintained module (the core Clipboard was deprecated and extracted from react-native, same story as AsyncStorage — knowing the extraction pattern is a currency signal). Both operations are async because they cross to a native module; getStringAsync resolves to the empty string when there's nothing to paste, and the modern API also handles images and URLs, not just text.

The consequences of "system-wide buffer" are the senior content. Writing: anything you put there outlives your app and is readable by the next app the user opens — copying a password or token is publishing it to the least trustworthy app on the device, and OS countermeasures (iOS's paste notification banner, Android 13's clipboard preview and auto-expiry) exist precisely because apps abused this. So sensitive copies should be deliberate, user-initiated, and ideally cleared after a timeout. Reading: don't poll the clipboard on launch to "helpfully" detect content — iOS visibly flags every read, and users experience surprise reads as spying. Copy-on-tap with a "Copied!" confirmation is the pattern; silent reads and writes are the anti-pattern.

Red flag: auto-reading the clipboard on app start, or parking secrets in it indefinitely — both are the behaviors the OS vendors literally built warning UI to shame.

Say it: "The clipboard is a system-wide buffer every app can read, so I write to it only on explicit user action, treat secrets in it as leaked, and never read it without the user asking me to."