我不能用setItem和AsyncStorage。以下是我的密码。因此,我从一个TextInput中获取状态名称,然后将其存储在AsyncStorage onPress of TouchableOpacity中。我搞错了:

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>
}
}我做错了什么?请帮帮忙。
发布于 2017-11-25 20:07:53
您需要从AsyncStorage中导入react-native
import { View, Text, StyleSheet, TextInput, TouchableOpacity , AsyncStorage} from 'react-native';您还需要保持asyncSetName函数的绑定。
取消注释这一行
this.asyncSetName = this.asyncSetName.bind(this)此外,正如@Chris所发现的,您需要更改类名以区别于AsyncStorage
发布于 2017-11-25 20:56:21
AsyncStorage中导入react-native,并按照他的建议保持绑定。AsyncStorage以外的其他内容@AppName是很常见的,所以您的最后一个键将是@AppName${this.state.name}。但同样,这是可选的,只是惯例而已。https://stackoverflow.com/questions/47489936
复制相似问题