我无法使用AsyncStorage设置setItem.以下是我的代码.所以我从TextInput获取状态名称,然后将其存储在AsyncStorage onPress of TouchableOpacity中.我收到错误: import React, { Component } from 'react';import { Vie
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>
}
}
我究竟做错了什么?请帮忙.
您需要从react-native导入AsyncStorageimport { View, Text, StyleSheet, TextInput, TouchableOpacity , AsyncStorage} from 'react-native';
您还需要保持asyncSetName函数的绑定
取消注释这一行
this.asyncSetName = this.asyncSetName.bind(this)
同样由@Chris发现,您需要将您的类名更改为与AsyncStorage不同
