reactjs – 无法在AsyncStorage中设置setItem
发布时间:2020-12-15 16:18:43 所属栏目:百科 来源:网络整理
导读:我无法使用AsyncStorage设置setItem.以下是我的代码.所以我从TextInput获取状态名称,然后将其存储在AsyncStorage onPress of TouchableOpacity中.我收到错误: import React,{ Component } from 'react';import { View,Text,StyleSheet,TextInput,TouchableO
我无法使用AsyncStorage设置setItem.以下是我的代码.所以我从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> } } 我究竟做错了什么?请帮忙. 解决方法
您需要从react-native导入AsyncStorage
import { View,TouchableOpacity,AsyncStorage} from 'react-native'; 您还需要保持asyncSetName函数的绑定 取消注释这一行 this.asyncSetName = this.asyncSetName.bind(this) 同样由@Chris发现,您需要将您的类名更改为与AsyncStorage不同 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |