I've been trying to solve this by using forceUpdate()
, but I when I tried it nothing really happened. I am still learning react native and I just experimented life cycles
of react native. Anything I tried didn't work at all.
class DrawerScreen extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4
};
AsyncStorage.getItem("username").then((value) => {
this.setState({
"username": value
})
});
}
saveData(value) {
AsyncStorage.setItem("username", value);
this.setState({
"username": value
});
}
componentDidUpdate() {
return fetch(`http://www.example.com/user-profile.php?username=${this.state.username}`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson,
images: responseJson[0].user_images
}, );
return responseJson;
})
.catch((error) => {
console.error(error);
});
}
render() {
return (
<
Container >
<
Content bounces = {
false
}
style = {
{
flex: 1,
backgroundColor: "#fff",
top: -1
}
} >
{
this.state.username != null ?
<
FlatList
data = {
this.state.dataSource
}
ItemSeparatorComponent = {
this.FlatListItemSeparator
}
renderItem = {
({
item
}) => < View >
<
Image source = {
{
uri: `http://www.example.co,/img/${item.banner}`
}
}
style = {
styles.drawerCover
}
/> <
Image square style = {
styles.profileImage
}
source = {
{
uri: `http://www.example.com/img/${item.profile}`
}
}
/> <
Text style = {
styles.nameText
} > {
item.first_name
} {
item.last_name
} < /Text>
<
/View>
}
keyExtractor = {
(item, index) => index
}
/> :
<
View >
<
Image source = {
drawerCover
}
style = {
styles.drawerCover
}
/> <
Image square style = {
styles.drawerImage
}
source = {
drawerImage
}
/> <
/View>
} <
List dataArray = {
datas
}
renderRow = {
data =>
<
ListItem
button
noBorder
onPress = {
() => this.props.navigation.navigate(data.route)
} >
<
Left >
<
Icon
active
name = {
data.icon
}
style = {
{
color: "#777",
fontSize: 26,
width: 30
}
}
/> <
Text style = {
styles.text
} > {
data.name
} <
/Text> <
/Left> {
data.types &&
<
Right style = {
{
flex: 1
}
} >
<
Badge
style = {
{
borderRadius: 3,
height: 25,
width: 72,
backgroundColor: data.bg
}
} >
<
Text
style = {
styles.badgeText
} >
{
`${data.types} Types`
} < /Text> <
/Badge> <
/Right>} <
/ListItem>} /
>
<
/Content> <
/Container>
);
}
}
The way I have it set it up is when a user logs in, then it will display their name,profile picture, and banner in the drawer navigation. For some reason, when I log out, the previous user is still there, but the current state is empty and it should display a default image.
logout = () =>{
this.setState({"username": null});
this.props.navigation.navigate('HomeScreen');
}