I can not setItemwith AsyncStorage. The following is my code. Therefore, I get the name in the state from TextInput, and then I want to save it in AsyncStorage onPress TouchableOpacity. I get an error message:

import React, { Component } from 'react';
import { View, Text, StyleSheet, TextInput, TouchableOpacity} from 'react-native';
class AsyncStorage extends Component {
constructor(props) {
super(props);
this.state = {
name:''
}
//this.asyncSetName = this.asyncSetName.bind(this) //tried this too.
};
asyncSetName(){
let name = this.state.name
AsyncStorage.setItem('name', name);
}
render(){
<View>
<TextInput
placeholder='Set Name here'
onChangeText={(name) => this.setState({name})}
value={this.state.name}
autoCorrect={false}
></TextInput>
<TouchableOpacity
onPress={this.asyncSetName.bind(this)}
>
<Text>SetName</Text>
</TouchableOpacity>
</View>
}
}
What am I doing wrong? Please, help.
source
share