React Native AsyncStorage
import React, { Component } from 'react'
import { View, Text, Button, AsyncStorage, TextInput, Keyboard } from 'react-native'
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
FirstName: '',
LastName: ''
}
}
SaveData = () => {
const { FirstName, LastName } = this.state
let myDetails = {
FirstName: FirstName,
LastName: LastName
}
AsyncStorage.setItem('Hello', JSON.stringify(myDetails))
console.log(myDetails)
Keyboard.dismiss()
}
GetData = async () => {
let getData = await AsyncStorage.getItem('Hello')
let Data = JSON.parse(getData)
console.log(Data)
}
render() {
return (
<View>
<TextInput
placeholder="Enter First Name"
onChangeText={FirstName => this.setState({ FirstName })}
/>
<TextInput
placeholder="Enter Last Name"
onChangeText={LastName => this.setState({ LastName })}
/>
<Button onPress={this.SaveData} title='SaveData'/>
<Button onPress={this.GetData} title='GetData'/>
</View>
);
}
}
No comments:
Post a Comment