Appearance
클립보드 텍스트 가져오기
getClipboardText
getClipboardText
는 클립보드에 저장된 텍스트를 가져오는 함수예요. 복사된 텍스트를 읽어서 다른 작업에 활용할 수 있어요.
시그니처
typescript
function getClipboardText(): Promise<string>;
반환 값
- Promise<string>
클립보드에 저장된 텍스트를 반환해요. 클립보드에 텍스트가 없으면 빈 문자열을 반환해요.
예제
클립보드의 텍스트 가져오기
tsx
import { getClipboardText } from '@apps-in-toss/web-framework';
import { Button, Text } from '@toss-design-system/mobile';
import { useState } from 'react';
// '붙여넣기' 버튼을 누르면 클립보드에 저장된 텍스트를 가져와 화면에 표시해요.
function PasteButton() {
const [text, setText] = useState('');
async function handleClick() {
try {
const clipboardText = await getClipboardText();
setText(clipboardText || '클립보드에 텍스트가 없어요.');
} catch (error) {
console.error('클립보드에서 텍스트를 가져오지 못했어요:', error);
}
}
return (
<div>
<Text>{text}</Text>
<Button onClick={handleClick}>붙여넣기</Button>
</div>
);
}
tsx
import { getClipboardText } from '@apps-in-toss/framework';
import { Button, Text } from '@toss-design-system/react-native';
import { useState } from 'react';
import { View } from 'react-native';
// '붙여넣기' 버튼을 누르면 클립보드에 저장된 텍스트를 가져와 화면에 표시해요.
function PasteButton() {
const [text, setText] = useState('');
async function handlePress() {
try {
const clipboardText = await getClipboardText();
setText(clipboardText || '클립보드에 텍스트가 없어요.');
} catch (error) {
console.error('클립보드에서 텍스트를 가져오지 못했어요:', error);
}
}
return (
<View>
<Text>{text}</Text>
<Button onPress={handlePress}>붙여넣기</Button>
</View>
);
}