4
我无法使用AsyncStorage setItem
。以下是我的代码。所以我从TextInput
那里得到这个名字,然后想把它存储在TouchableOpacity
的AsyncStorage
onPress
。我收到提示:无法在AsyncStorage中设置项目
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>
}
}
我在做什么错?请帮忙。
谢谢。我是否必须在构造函数(props)中绑定这个'this.asyncSetName = this.asyncSetName.bind(this)',并且像这样调用它:'onPress = {this.asyncSetName.bind(this)}'。 为什么我需要将它绑定两次? – Somename
欢迎您......关于绑定,您需要绑定一次,我更喜欢使用构造函数中的一个 –