In my React Native App actually i need implement a simple Logout with navigation and redux, but i have an error when i dispatch the logout action from my Home page (on press Logout button), because in Home i render the current user and not exists in that moment.
I need dispatch Logout (delete current user) and after navigate to the Login page without render again Home or to know how implement componentWillMount before render.... How ?
my resum code: (Login works, i dont put the login code)
App.tsx
import React, {useState} from 'react';
import { NavigationContainer, StackActions } from '@react-navigation/native';
//... many imports
const store = createStore(rootReducer);
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { loading: true };
}
async componentDidMount() {
//... some code
}
render(){
if (this.state.loading) {
return (
<Root>
<AppLoading />
</Root>
);
}
else {
return (
<Provider store={store}>
<Root>
<NavigationContainer>
<Navigator/>
</NavigationContainer>
</Root>
</Provider>
);
}
}
}
Navigator.tsx
import React from 'react'
import { createStackNavigator } from '@react-navigation/stack'
import {connect} from 'react-redux'
import Login from '../Views/Login'
import Home from '../Views/Home'
const Stack = createStackNavigator();
const Navigator= (props) =>{
const {users} = props;
return (
<Stack.Navigator>
{console.log('Navigator here, '+users.current)}
{users.current ===null ?
(<>
<Stack.Screen name="Login" component={Login} options={{headerShown:false}}/>
</>)
: (<>
<Stack.Screen name="Home" component={Home} options={{headerShown:false}}/>
</>)
}
</Stack.Navigator>
);
}
const mapStateToProps=(state)=>{
const {users} = state;
return {users};
}
export default connect(mapStateToProps)(Navigator);
Home.tsx (navigate after Logout ?)
import React from 'react'
import {Text, Container, Content, View, Button, Icon} from 'native-base'
import Styles from './Styles/HomeStyles'
import {connect} from 'react-redux'
import {Auth} from '../Services/Auth/AuthService'
const Home =(props) => {
const {navigation, users} = props;
return(
<Container >
<Content >
<View style={Styles.content}>
<Text>Hi {users.current.id}</Text>
<Button onPress={()=>Logout(users.current.id)} >
<Icon name='exit' />
</Button>
</View>
</Content>
</Container>
);
async function Logout(id:string){
console.log('Function start');
await Auth.LogoutUser(id, props);
navigation.navigate('Login');
}
}
const mapStateToProps=(state)=>{
const {users} = state;
return {users};
}
export default connect(mapStateToProps)(Home);
UserReducer.tsx
const Initial_State={
current:null
}
export function UserReducer(state=Initial_State, action:any){
switch (action.type){
case 'ADD_USER':
return{
...state,
current: action.user
};
case 'DELETE_USER':
return{
...state,
current: null
};
default:
return state;
}
}
AuthService.tsx (here the dispatch)
export const Auth={
LoginUser,
LogoutUser
}
async function LogoutUser(id: string, props: any){
await props.dispatch({type : 'DELETE_USER', id});
}
async function LoginUser(AuthData:IAuthData, props: any){
//...some code
}