File tree Expand file tree Collapse file tree 1 file changed +18
-10
lines changed Expand file tree Collapse file tree 1 file changed +18
-10
lines changed Original file line number Diff line number Diff line change 1
1
import { useEffect , useState } from 'react'
2
2
import { Clipboard } from 'react-native'
3
3
4
+ type Listener = ( content : string ) => void
5
+ const listeners = new Set < Listener > ( )
6
+
7
+ function setString ( content : string ) {
8
+ Clipboard . setString ( content )
9
+ listeners . forEach ( listener => listener ( content ) )
10
+ }
11
+
4
12
export default function useClipBoard ( ) {
5
13
const [ data , updateClipboardData ] = useState ( '' )
6
14
7
- async function updateClipboard ( ) {
8
- const content = await Clipboard . getString ( )
9
- updateClipboardData ( content )
10
- }
11
-
15
+ // Get initial data
12
16
useEffect ( ( ) => {
13
- updateClipboard ( )
17
+ Clipboard . getString ( ) . then ( updateClipboardData )
14
18
} , [ ] )
15
19
16
- function setString ( content : string ) {
17
- Clipboard . setString ( content )
18
- updateClipboardData ( content )
19
- }
20
+ // Listen for updates
21
+ useEffect ( ( ) => {
22
+ listeners . add ( updateClipboardData )
23
+
24
+ return ( ) => {
25
+ listeners . delete ( updateClipboardData )
26
+ }
27
+ } , [ ] )
20
28
21
29
return [ data , setString ]
22
30
}
You can’t perform that action at this time.
0 commit comments