Once I remove user token, user redirects to Login Page. However if I login with other user, the main page still shows the previous user information.
This is because I didn't refresh the Main page. How can I re-initialize(?) Main Page manually in react-navigation?
MainPage(Logged in as 'matt') -> Logout -> LoginPage
-> (here I want to refresh MainPage so it can be loaded once new user login)
-> MainPage(Should be logged in as other user 'kobe')
MainPage receive user JSON data via componentWillMount
componentWillMount() {
// retrieve user data
this.props.retrieveCurrentUser(this.props.token);
}
Just in case you need my code...
1) This is Root navigation
const RootTabNavigator = TabNavigator ({
Auth: {
screen: AuthStackNavigator,
},
Welcome: {
screen: WelcomeScreen,
},
Main: {
screen: MainTabNavigator,
},
}, {
2) This is Auth Stack Navigator (Login Page)
export default StackNavigator ({
Autho: {
screen: AuthScreen,
},
SignUp: {
screen: SignUpScreen,
},
SignUpBio: {
screen: SignUpUserBioScreen,
},
SignUpUsername: {
screen: SignUpUsernameScreen,
},
}, {
header: null,
headerMode: 'none',
navigationOptions: {
header: null
},
lazy: true
});
3) This is Main TabNavigator
export default TabNavigator(
{
Feed: {
screen: FeedScreen,
},
Stylebook: {
screen: StylebookScreen,
},
Wardrobe: {
screen: WardrobeScreen,
},
Noti: {
screen: NotificationScreen,
},
Menu: {
screen: MenuScreen,
},
}, {
...
}
If set to true, the browser will do a complete page refresh from the server and not from the cached version of the page. import React from 'react'; function App() { function refreshPage() { window. location. reload(false); } return ( <div> <button onClick={refreshPage}>Click to reload!
React Native provides an individual RefreshControl component specifically to implement pull-to-refresh easily in a React Native app. If you're rendering a list of data using the ScrollView or FlatList component, you can use RefreshControl to add the pull-to-refresh capability.
setInterval(() => { window. location. reload(); }, 5000);
If you use flux or redux to manage your state then you update your new state (e.g. new user name and whatever other data changes) in the store and everything gets updated.
If you don't use a state management solution then you can pass parameters in the navigate
function. So when you navigate to the main page after the login, pass a flag or something that lets the main page know it needs to update.
This is the example from the linked doc:
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<View>
<Text>Hello, Chat App!</Text>
<Button
onPress={() => navigate('Chat', { user: 'Lucy' })}
title="Chat with Lucy"
/>
</View>
);
}
}
class ChatScreen extends React.Component {
// Nav options can be defined as a function of the screen's props:
static navigationOptions = ({ navigation }) => ({
title: `Chat with ${navigation.state.params.user}`,
});
render() {
// The screen's current route is passed in to `props.navigation.state`:
const { params } = this.props.navigation.state;
return (
<View>
<Text>Chat with {params.user}</Text>
</View>
);
}
}
And in your case, when you navigate to main page:
navigate('MainPage', { token: '<new token>' })}
and in MainPage:
componentWillReceiveProps(nextProps) {
if (nextProps.navigation.state.params.token) {
this.props.retrieveCurrentUser(this.props.token);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With