Recommand · October 14, 2021 0

React Native: the action navigate with payload was not handled by any navigator

I am trying to navigate from login screen to profile screen after authenticating email and password.
Everything else works. I am able to get token from auth api, store it retrieve it.
After that I am not able to navigate to the next screen, which throws error

Here’s my code:

import * as React from 'react';
import { useState } from 'react';
import { ActivityIndicator, StatusBar, StyleSheet, TextInput, TouchableOpacity, Image, TouchableHighlight } from 'react-native';
import { Text, View } from '../components/Themed';
import * as SecureStore from 'expo-secure-store';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { createDrawerNavigator } from '@react-navigation/drawer';
import navigation from '../navigation';

export default function LoginScreen({navigation}) {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [data, setData] = useState([]);

  const doUserLogIn = async() => {
    try {
      const response = await fetch(''+email+'&password='+password, {
        method: 'POST',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json'
      const json = await response.json();
    } catch (e) {
    } finally {
        await SecureStore.setItemAsync('token', JSON.stringify(data.token));
      } catch(e) {alert('Error2:'+e); }
  return (
    <View  style={styles.container}>
    <StatusBar style="auto" />
    <View style={styles.inputView}>
        onChangeText={(email) => setEmail(email)}

    <View style={styles.inputView}>
        onChangeText={(password) => setPassword(password)}

    <TouchableOpacity style={styles.loginBtn} onPress={() => doUserLogIn()}>
      <Text style={styles.loginText}>Anmelden</Text>

Below is the code for my Drawer navigation:

const Drawer = createDrawerNavigator();

function RootNavigator() {
  return (
    <Drawer.Navigator initialRouteName="Home">
      <Drawer.Screen name="Anmelden" component={LoginScreen} />
      <Drawer.Screen name="Profil" component={ProfileScreen} />
      <Drawer.Screen name="Schichten" component={ShiftsScreen} />
      <Drawer.Screen name="Arbeitszeitbericht" component={TimelogScreen} />

The error means that ‘Profile’ is not in the same stack of screens as this current page. If you have your stacks separated by "Logged In", then your login stack will not have access to your "Logged In" stack. You could have a condition in place instead:

userLoggedIn ? LoggedInStack : LoginStack

So when the user logs in, it automatically switched to the LoggedInStack which would have "Profile" as its first screen so it goes there directly.