I have a problem calling this.setState({}) in componentDidMount(). If I call this.setState({}) in comonentDidMount() it seems that it isn´t updating the state beacause if I call my function to fetch data from firestore the state is "" and not the set one in componentDidMount()
constructor:
this.state = {
userid: '',
username: '',
useremail: ''
}
componentDidMount:
AsyncStorage.getItem('@userProfileDocID', function (error, result){
if (result != null) {
this.setState({ userid: result }, () => {
alert(this.state.userid);
});
}
}.bind(this));
fire.collection('User').doc(this.state.userid).get().then(function (doc) {
if (doc.exists) {
this.setState({
username: doc.name,
useremail: doc.email
});
}
}.bind(this));
render:
return (
<SafeAreaView style={{ alignItems: 'center' }}>
<View style={{
borderRadius: 5,
width: 350,
height: 350,
shadowColor: 'black',
shadowOffset: {width: 0, height: 0},
shadowOpacity: 0.1,
shadowRadius: 1.5,
backgroundColor: 'white',
alignItems: 'center',
padding: 20
}}>
<Icon name="ios-contact" color={'#d0d0d0'} size={100} />
<Text>UserID: {this.state.userid}</Text> {/* <-- This Works (gets updated) */}
<Text>Username: {this.state.username}</Text>
<Text>Email: {this.state.useremail}</Text>
</View>
</SafeAreaView>
)