네이티브 저장소 이용하기
Storage
Storage 로 네이티브의 저장소를 사용할 수 있어요.
시그니처
Storage: {
getItem: typeof getItem;
setItem: typeof setItem;
removeItem: typeof removeItem;
clearItems: typeof clearItems;
}프로퍼티
- getItemtypeof getItem
모바일 앱의 로컬 저장소에서 아이템을 가져오는 함수예요. 자세한 내용은 getItem을 참고하세요.
- setItemtypeof setItem
모바일 앱의 로컬 저장소에 아이템을 저장하는 함수예요. 자세한 내용은 setItem을 참고하셰요.
- removeItemtypeof removeItem
모바일 앱의 로컬 저장소에서 아이템을 삭제하는 함수예요. 자세한 내용은 removeItem을 참고하세요.
- clearItemstypeof clearItems
모바일 앱의 로컬 저장소를 초기화하는 함수예요. 자세한 내용은 clearItems을 참고하세요.
예제 앱 체험하기
apps-in-toss-examples 저장소에서 with-storage 코드를 내려받거나, 아래 QR 코드를 스캔해 직접 체험해 보세요.
setItem
setItem 함수는 모바일 앱의 로컬 저장소에 문자열 데이터를 저장해요. 주로 앱이 종료되었다가 다시 시작해도 데이터가 유지되어야 하는 경우에 사용해요.
시그니처
function setItem(key: string, value: string): Promise<void>;파라미터
- key필수 · string
저장할 아이템의 키를 입력해요.
- value필수 · string
저장할 아이템의 값을 입력해요.
반환 값
- Promise<void>
아이템을 성공적으로 저장하면 아무 값도 반환하지 않아요.
예제
my-key에 아이템 저장하기
import { Storage } from '@apps-in-toss/web-framework';
const KEY = 'my-key';
async function handleSetStorageItem(value) {
const storageValue = await Storage.setItem(KEY, value);
}
async function handleGetStorageItem() {
const storageValue = await Storage.getItem(KEY);
return storageValue;
}
async function handleRemoveStorageItem() {
await Storage.removeItem(KEY);
}import { Storage } from '@apps-in-toss/web-framework';
import { Button, Text } from '@toss/tds-mobile';
import { useState } from 'react';
const KEY = 'my-key';
function StorageTestPage() {
const [storageValue, setStorageValue] = useState<string | null>(null);
async function handleSet() {
await Storage.setItem(KEY, 'my-value');
}
async function handleGet() {
const storageValue = await Storage.getItem(KEY);
setStorageValue(storageValue);
}
async function handleRemove() {
await Storage.removeItem(KEY);
}
return (
<>
<Text>{storageValue}</Text>
<Button onClick={handleSet}>저장하기</Button>
<Button onClick={handleGet}>가져오기</Button>
<Button onClick={handleRemove}>삭제하기</Button>
</>
);
}import { Storage } from '@apps-in-toss/framework';
import { Button, Text } from '@toss/tds-react-native';
import { useState } from 'react';
const KEY = 'my-key';
function StorageTestPage() {
const [storageValue, setStorageValue] = useState<string | null>(null);
async function handleSet() {
await Storage.setItem(KEY, 'my-value');
}
async function handleGet() {
const storageValue = await Storage.getItem(KEY);
setStorageValue(storageValue);
}
async function handleRemove() {
await Storage.removeItem(KEY);
}
return (
<>
<Text>{storageValue}</Text>
<Button onPress={handleSet}>저장하기</Button>
<Button onPress={handleGet}>가져오기</Button>
<Button onPress={handleRemove}>삭제하기</Button>
</>
);
}getItem
getItem 함수는 모바일 앱의 로컬 저장소에서 문자열 데이터를 가져와요. 주로 앱이 종료되었다가 다시 시작해도 데이터가 유지되어야 하는 경우에 사용해요.
시그니처
function getItem(key: string): Promise<string | null>;파라미터
- key필수 · string
가져올 아이템의 키를 입력해요.
반환 값
- Promise<string | null>
지정한 키에 저장된 문자열 값을 반환해요. 값이 없으면
null을 반환해요.
예제
my-key에 저장된 아이템 가져오기
import { Storage } from '@apps-in-toss/web-framework';
const KEY = 'my-key';
async function handleGetItem() {
const storageValue = await Storage.getItem(KEY);
return storageValue;
}import { Storage } from '@apps-in-toss/web-framework';
import { Button } from '@toss/tds-mobile';
const KEY = 'my-key';
function StorageClearButton() {
async function handleGet() {
const storageValue = await Storage.getItem(KEY);
setStorageValue(storageValue);
}
return <Button onClick={handleGet}>가져오기</Button>;
}import { Storage } from '@apps-in-toss/framework';
import { Button } from '@toss/tds-react-native';
const KEY = 'my-key';
function StorageClearButton() {
async function handleGet() {
const storageValue = await Storage.getItem(KEY);
setStorageValue(storageValue);
}
return <Button onPress={handleGet}>가져오기</Button>;
}removeItem
removeItem 함수는 모바일 앱의 로컬 저장소에서 특정 키에 해당하는 아이템을 삭제해요.
시그니처
declare function removeItem(key: string): Promise<void>;파라미터
- key필수 · string
삭제할 아이템의 키를 입력해요.
반환 값
- Promise<void>
아이템을 삭제하면 아무 값도 반환하지 않아요.
예제
my-key에 저장된 아이템 삭제하기
import { Storage } from '@apps-in-toss/web-framework';
const KEY = 'my-key';
async function handleRemoveItem() {
await Storage.removeItem(KEY);
}import { Storage } from '@apps-in-toss/web-framework';
import { Button } from '@toss/tds-mobile';
const KEY = 'my-key';
function StorageClearButton() {
async function handleRemove() {
await Storage.removeItem(KEY);
}
return <Button onClick={handleRemove}>삭제하기</Button>;
}import { Storage } from '@apps-in-toss/framework';
import { Button } from '@toss/tds-react-native';
const KEY = 'my-key';
function StorageClearButton() {
async function handleRemove() {
await Storage.removeItem(KEY);
}
return <Button onPress={handleRemove}>삭제하기</Button>;
}clearItems
clearItems 함수는 모바일 앱의 로컬 저장소의 모든 아이템을 삭제해요.
시그니처
declare function clearItems(): Promise<void>;반환 값
- Promise<void>
아이템을 삭제하면 아무 값도 반환하지 않고 저장소가 초기화돼요.
예제
저장소 초기화하기
import { Storage } from '@apps-in-toss/web-framework';
async function handleClearItems() {
await Storage.clearItems();
console.log('Storage cleared');
}import { Storage } from '@apps-in-toss/web-framework';
import { Button } from '@toss/tds-mobile';
function StorageClearButton() {
async function handleClick() {
await Storage.clearItems();
console.log('Storage cleared');
}
return <Button onClick={handleClick}>저장소 초기화</Button>;
}import { Storage } from '@apps-in-toss/framework';
import { Button } from '@toss/tds-react-native';
function StorageClearButton() {
async function handlePress() {
await Storage.clearItems();
console.log('Storage cleared');
}
return <Button onPress={handlePress}>저장소 초기화</Button>;
}