Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

Usage

Async Storage can only storestring data. In order to store object data,you need to serialize it first. For data that can be serialized to JSON, you canuseJSON.stringify() when saving the data andJSON.parse() when loading thedata.

Importing

importAsyncStoragefrom'@react-native-async-storage/async-storage';

Storing data

setItem() is used both to add new data item (when no data for given keyexists), and to modify existing item (when previous data for given key exists).

Storing string value

conststoreData=async(value)=>{
try{
awaitAsyncStorage.setItem('my-key', value);
}catch(e){
// saving error
}
};

Storing object value

conststoreData=async(value)=>{
try{
const jsonValue=JSON.stringify(value);
awaitAsyncStorage.setItem('my-key', jsonValue);
}catch(e){
// saving error
}
};

Reading data

getItem returns a promise that either resolves to stored value when data isfound for given key, or returnsnull otherwise.

Reading string value

constgetData=async()=>{
try{
const value=awaitAsyncStorage.getItem('my-key');
if(value!==null){
// value previously stored
}
}catch(e){
// error reading value
}
};

Reading object value

constgetData=async()=>{
try{
const jsonValue=awaitAsyncStorage.getItem('my-key');
return jsonValue!=null?JSON.parse(jsonValue):null;
}catch(e){
// error reading value
}
};

More

For more examples,head over to API section.


[8]ページ先頭

©2009-2025 Movatter.jp