Her uygulamada bazı bilgilerin kayıtlı kalması mutlaka gerekebilir. Bu işlem için veritabanı oluşturmakta bir alternatif olabilir fakat küçük bilgiler için kullanışlı olmayabilir. Örnek vermek gerekirse bir login ekranında beni hatırla özelliği için AsyncStorage kullanılabilir ve kullanımı oldukça kolay. Doğrudan örnek kodunu paylaşıyorum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import React, { Component } from 'react'; import { View, Text, AsyncStorage, TouchableOpacity, StyleSheet } from 'react-native'; export default class App extends Component { constructor(props) { super(props); this.state = { }; } // local'de kayıtlı veriyi alma getVeri = async () => { // 3 const veri = await AsyncStorage.getItem('@veri') console.log(veri) } // local'e veriyi kaydetme setVeri = async () => { AsyncStorage.setItem('@veri', "yusufpamukcu.com"); } render() { return ( <View style={styles.container}> <TouchableOpacity style={styles.button} onPress={() => { this.setVeri() }} > <Text style={{ color: '#272828', fontSize: 22 }}>Veriyi Kaydet</Text> </TouchableOpacity> <TouchableOpacity style={styles.button} onPress={() => { this.getVeri() }} > <Text style={{ color: '#272828', fontSize: 22 }}>Veriyi Al</Text> </TouchableOpacity> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#ccafaf50' }, button: { width: 350, height: 50, margin: 5, backgroundColor: '#F5B971', justifyContent: 'center', alignItems: 'center', borderRadius: 5, borderWidth: 1, borderColor: '#27282850' } }); |
İlk Yorumu Siz Yapın