0
我是新来对本机做出反应的,我只是在学习反应原生现在,我试图通过单击注册按钮来存储TextInput中的值并将其存储到AsyncStorage中。该值通过单击获取Button.I无法从AsyncStorage.getItem('值')获得值,后来我认为其他数据存储在AsyncStorage.setItem('name','value')。有人看这段代码并告诉我我的错误答案。无法在React本机中使用AsyncStorage存储和检索数据
export default class AsyncStorageEample extends Component
{
constructor(props)
{
super(props)
this.state= { name:'', email:'',mobile:''}
this.onRegisterPress=this.onRegisterPress.bind(this)
this.onRetreive=this.onRetreive.bind(this)
}
onRegisterPress()
{
let name=this.state.name
let email=this.state.email
let mobile=this.state.mobile
AsyncStorage.setItem('name',name)
AsyncStorage.setItem('email',email)
AsyncStorage.setItem('mobile',mobile)
this.setState({name: name, email:email,mobile:mobile})
}
onRetreive()
{
var user;
user= AsyncStorage.getItem('name').then((value) => this.setState({ 'name': value }))
Alert.alert('Name:'+user);
}
render() {
return (
<View style={styles.container}>
<View style={styles.Signupform}>
<Text style={styles.textStyle}> Name </Text>
<TextInput style={styles.tInRegister} onChangeText={(text)=> this.setState({name: text})} value = {this.state.name}/>
<Text style={styles.textStyle}> Email </Text>
<TextInput style={styles.tInRegister} onChangeText={(text)=> this.setState({email: text})} value= {this.state.email} />
<Text style={styles.textStyle}> Mobile </Text>
<TextInput style={styles.tInRegister} onChangeText={(text)=> this.setState({mobile: text})} vaue= {this.state.mobile} />
<Button
onPress={this.onRegisterPress.bind(this)}
title="Register"
color="#841584"
/>
<Button
style={styles.get}
onPress={this.onRetreive.bind(this)}
title="Get"
color="#841584"/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
tInRegister: {
fontSize: 16,
height: 40,
color: '#800080'
},
textStyle: {
fontSize: 16,
color: '#800080'
},
Signupform: {
width: 400,
height: 100,
justifyContent: 'center',
marginTop: 10,
marginLeft: 10,
marginRight: 10
},
get:
{
marginTop:20,
},
});
AppRegistry.registerComponent('AsyncStorageEample',() => AsyncStorageEample);
@sowmya被使用。我不知道你是如何错过它的。 –
谢谢。我有点困惑 – saiRam89
好。快乐的编码。 –