Navigating to a different screen within a separate navigator using react-navigation 6

My application has a complex structure with three levels of nested navigators. I'm facing an issue where I can't navigate to the "Home" screen from the deepest level of navigation, specifically the "AccountDetails" screen. Clicking on the button seems to do nothing, no logs or navigation occurs. It appears to be a potential bug in the library, although I cannot completely rule out the possibility that I may have missed something. Any insights on what might be causing this?

Below is the code snippet:

type RootStackParamList = {
  SignIn: undefined;
  SignUp: undefined;
  Home: undefined;
};

type HomeTabParamList = {
  BudgetTab: undefined;
  AccountsTab: undefined;
  SettingsTab: undefined;
};

type AccountStackParamList = {
  Accounts: undefined;
  AccountDetails: undefined;
};

const Stack = createNativeStackNavigator<RootStackParamList>();
const Tab = createBottomTabNavigator<HomeTabParamList>();
const AccountStack = createNativeStackNavigator<AccountStackParamList>();

const AccountDetails = ({ navigation }: any) => {
  return (
    <View>
      <Text>Account Details</Text>
      <Button
        onPress={() => {
          navigation.navigate("Home");
        }}
      >
        Go home
      </Button>
    </View>
  );
};

const Accounts = ({ navigation }: any) => {
  return (
    <View>
      <Text>Accounts</Text>
      <Button
        onPress={() => {
          navigation.navigate("AccountDetails");
        }}
      >
        View details
      </Button>
    </View>
  );
};

const BudgetTab = ({ navigation }: any) => {
  return (
    <View>
      <Button
        onPress={() => {
          navigation.navigate("SettingsTab");
        }}
      >
        Sign Up
      </Button>
      <Text>Budget</Text>
    </View>
  );
};
const SettingsTab = () => {
  return (
    <View>
      <Text>Settings</Text>
    </View>
  );
};
const AccountsTab = () => {
  return (
    <AccountStack.Navigator>
      <AccountStack.Screen name="Accounts" component={Accounts} />
      <AccountStack.Screen name="AccountDetails" component={AccountDetails} />
    </AccountStack.Navigator>
  );
};

const SignIn = () => {
  return (
    <View>
      <Text>Sign In</Text>
    </View>
  );
};
const SignUp = () => {
  return (
    <View>
      <Text>Sign Up</Text>
    </View>
  );
};
const HomeTabs = () => {
  return (
    <Tab.Navigator>
      <Tab.Screen name="BudgetTab" component={BudgetTab} />
      <Tab.Screen name="AccountsTab" component={AccountsTab} />
      <Tab.Screen name="SettingsTab" component={SettingsTab} />
    </Tab.Navigator>
  );
};

export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeTabs} />
        <Stack.Screen name="SignIn" component={SignIn} />
        <Stack.Screen name="SignUp" component={SignUp} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

Answer №1

Utilize the popToTop function to navigate to the top of the current tab.

navigation.popToTop()

To access a different tab (screen), specify it within the navigate method.

navigation.navigate('Home', { screen: 'BudgetTab' });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Assistance required in configuring eslint for a monorepo with Yarn 3 and TypeScript

I've been struggling to configure ESlint in my monorepo while using Yarn 3.2.4 as the package manager. To see an example project, check out this GitHub repository. Here is the structure of the project: /monorepo ├── /configs │ ├── /esl ...

Update the sorting icon in the data grid using Material-UI

I'm looking for a way to replace the current icon in the Material UI data grid with a different one. Below is the code I'm using to implement the data grid component: https://i.sstatic.net/griFN.png import { DataGrid, DataGridProps, GridColDef,G ...

The proper method to type the onChange handler in TypeScript

I'm dealing with a simple controlled input setup that looks like this: <textarea name="description" onChange={updateEdit} value={edit} placeholder={edit} /> Here's my custom updateEdit handler: const updateEdit = (evt: Re ...

*ngIf - use a property that requires multiple settings

In my Angular 6 project, I have implemented a WIJMO grid in the template that pulls data from a database table. Each row in the grid should display either a delete button or an un-delete button based on the condition specified using *ngIf else: <wj-fle ...

Using navigateByUrl() to pass a query parameter

When I click on an icon, I want to navigate to a new page. Here's the code snippet: this.prj = e.data.project_number; this.router.navigateByUrl('/dashboard/ProjectShipment/634'); Instead of hardcoding the query parameter 000634, I need ...

Issue: The module is not compatible with the "node" engine. It requires version ">= 14.20.0" but is receiving "14.17.4"

In the midst of working on a challenging branch, I struggled to organize my dependencies. However, after lots of effort, I finally managed to get everything up and running smoothly, with my GitLab pipelines passing successfully. Just so you know, the proje ...

How can Angular JS handle multiple validators being triggered at once?

Hey there, I'm currently working with validators in my Angular form setup. Here's a snippet of how it looks - I utilize Validators.compose to combine custom validators. The errors from these validators are then displayed on the HTML component. My ...

Attempting to conceal certain elements based on the current route in Angular 5

Trying to create a dynamic navigation system where different items are displayed based on the website path. For example, if the path is http://mywebsite.com/path, certain navigation items should be hidden while others are shown. My current setup includes. ...

Exporting Types in an NPM Package: Best Practices

Struggling to create a private NPM package for internal use within my company's Nodejs applications using Typescript. Currently, I have a basic proof of concept with some constants, but I'm having trouble structuring it in a way that is importabl ...

Verify in Typescript if there is at least one value supplied

Looking for a solution: function takeOneOfOrThrow(firstOptionalVariable : string | undefined, secondOptionalVariable : string | undefined) { let result : string; if (!firstOptionalVariable && !secondOptionalVariable) { throw new E ...

Tips for ensuring the reliability of unit tests when testing an OnPush component in Angular with fixture.detectChanges()

I developed a project where I implemented a Component that fetches data asynchronously from a service and displays it to the user. The Component code is as follows: @Component({ changeDetection: ChangeDetectionStrategy.Default, selector: 'test-co ...

Encountering an issue while trying to import the instanceMethods function within Sequelize

In a file, I have written a function and exported it like this: export function foo(params...) { // do something } When initializing the model, I imported the function in the following manner: import { foo } from "../path/..." instanceMethods: { foo ...

Enhance Tuple in Angular Component Properties

When using React, state variables can be created like this: const SomeComponent = ({ someProp }) => { const [value, setValue] = useState<boolean>(false); } I wonder if there is a similar way to achieve the spread of a tuple within an Angular co ...

Maintaining the generic types in mapped types in TypeScript

In my current project, I have a unique design where a class contains instance methods that act as handlers, each representing a specific operation. These handlers take a reference as input and assign the output to a second parameter. To simplify this proce ...

Can we limit a specific key to only accept a specific value?

Is it possible to restrict the type of a certain key to a certain value in TypeScript for an object? For instance, consider the following example: interface ValueType1 { key1: string, key2: string, } interface ValueType2 { key3: number, ...

The custom function is not compatible with any of the available overloads

I'm setting up an event listener that triggers a function to update the state when the event occurs. import React, {useState, useRef, useLayoutEffect} from 'react'; const [width, setWidthIn] = useState<number>(0) const ref = useRef< ...

Encountering problem in building React Native Android CLI: react-native-vision-camera: compileDebugKotlin has failed

Upon adding the package "react-native-vision-camera": "^4.0.5" to my project, I encountered an issue while trying to build the Android apk. The error can be seen here android/build.gradle react-native-version ...

Formatting Time in Angular 2 Using Typescript

Upon reviewing my date source, I found the following: TimeR ="2017-02-17 19:50:11 UTC"; Within the HTML file, I implemented this code snippet <span class="text-lg">{{TimeR | date: 'hh:mm'}}</span> The current output displays the t ...

Utilize dynamic injection of JavaScript code within Angular 5 for enhanced functionality

For nearly a year now, I've been immersed in a project where we started with Angular 2 during its rc versions, and we've since made our way up to version 5. One requirement for our app is the ability to transpile TypeScript code into JavaScript ...

The directive for angular digits only may still permit certain characters to be entered

During my exploration of implementing a digits-only directive, I came across a solution similar to my own on the internet: import { Directive, ElementRef, HostListener } from '@angular/core'; @Directive({ selector: '[appOnlyDigits]' ...