React Native projelerin veri saklamayı sağlayan Async Storage paketini aşağıdaki komutla projeye eklenir;
// npm ile npm install @react-native-async-storage/async-storage // Yarn ile yarn add @react-native-async-storage/async-storage // Expo CLI: expo install @react-native-async-storage/async-storage [/code] iOs üzerinde uygulama geliştiriyorsanız aşağıdaki komutu çalıştırın; Kullanılacak sayfaya import etme; setItem ile veri saklama; const storeData = async (value) => { try { await AsyncStorage.setItem('@storage_Key', value) } catch (e) { // saving error } }
Obje saklama
const storeData = async (value) => { try { const jsonValue = JSON.stringify(value) await AsyncStorage.setItem('@storage_Key', jsonValue) } catch (e) { // saving error } }
Saklanan değeri çağırma ve okuma;
const getData = async () => { try { const value = await AsyncStorage.getItem('@storage_Key') if(value !== null) { // value previously stored } } catch(e) { // error reading value } }
Saklanan objeyi çağırma ve okuma;
const getData = async () => { try { const jsonValue = await AsyncStorage.getItem('@storage_Key') return jsonValue != null ? JSON.parse(jsonValue) : null; } catch(e) { // error reading value } }
İlk Yorumu Siz Yapın