I have stored data user with AsyncStorage when i want to get it, it renders null in alert and the above error in the console,i don't know why.
I can not see why it returns null with user but token all is well
profile.js :
import React, {Component} from 'react';
import { StyleSheet, Text, View, Image, TouchableOpacity,AsyncStorage}
from 'react-native';
import { Container, Content, Header,Right, Left, Body} from 'native-
base';
import Feather from 'react-native-vector-icons/Feather';
export default class Profile extends Component {
constructor (props){
super(props);
this.state = {
user: null
}
}
componentDidMount(){
this.fetchData();
}
fetchData = async() =>{
let username = await AsyncStorage.getItem('user')
this.setState({
user: username
})
alert(user)
console.log(user)
};
render(){return (....)}
here when i stock the data user and token.
Signin.js :
signInMethod = () => {
this.setState({
showProgress : true
});
return fetch(url, {
method: "POST",
headers: {
'Accept': "application/json",
'Content-Type': "application/json",
},
body : JSON.stringify({
email : this.state.userEmail,
password : this.state.userPassword
})
})
.then((response) => response.json())
.then((res) => {
//if there was an error
if(res.error){
//Alert.alert("رسالة","المرجو التأكد من صحة البيانات");
this.setState({
error: 'المرجو التأكد من صحة البيانات'
});
return;
}
//if there was no error then store the jwt in the localstorage
this.setState({
customerData : res.customer
});
if (this.state.customerData && res.token) {
//we store the user data to be used localy
AsyncStorage.setItem('user',JSON.stringify(res.customer));
AsyncStorage.setItem('jwt', res.token);
AsyncStorage.getItem('user');
updateLogin(res.token);
this.props.navigation.navigate('Home');
}
})
.catch((err) => {
Alert.alert("المرجو اعادة المحاولة لاحقا");
})
}
help please.
I resolve the problem :
profile.js:
fetchData = async() =>{
let userData = await AsyncStorage.getItem('user', (value) => {
JSON.parse(value);
});
//console.log(userData)
this.setState({
user: userData
})
alert(this.state.user)
//console.log(this.state.user.name);
}
the problem what i find now is when i want to show data user such as name, email...
<Text>{this.state.user.name}<Text>
I get this error : Cannot read property 'name' of null