Skip to content

Instantly share code, notes, and snippets.

@michalchudziak
Last active March 1, 2019 09:17
Show Gist options
  • Select an option

  • Save michalchudziak/f99d403abe90f11e8146c6f75416a0c8 to your computer and use it in GitHub Desktop.

Select an option

Save michalchudziak/f99d403abe90f11e8146c6f75416a0c8 to your computer and use it in GitHub Desktop.
/* @flow */
import React, {useState, useEffect} from 'react';
import {NativeModules} from 'react-native';
export default function useClipboard() {
const [contents, setContents] = useState('');
useEffect(() => {
getClipboardContents();
}, []);
const getClipboardContents = async () => {
const content = await NativeModules.Clipboard.getString();
setContents(content);
};
const setClipboardContents = (content) => {
NativeModules.Clipboard.setString(content);
setContents(content);
};
return [contents, setClipboardContents];
};
@peterleilei86
Copy link

Do you need to pass in an empty array as the second argument to useEffect to make sure this effect only happens on componentDidMount and componentWillUnmount?

@michalchudziak
Copy link
Author

Good point @peterdev6, I'll update the contents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment