Experiencing difficulty with nested navigators in react navigation, struggling with

I am currently working on a tab navigator with Home and Profile screens. Within the Home screen, there is a stack navigator containing the RestaurantDetails and RestaurantList screens.

My issue lies with a Button located in the RestaurantList screen. I am trying to navigate to the RestaurantDetails screen.

This is the code for Button.tsx:

const Button = ({name}) => {
  const navigation = useNavigation()
  const onPress = () => {
    navigation.navigate("RestaurantDetails", {name})
  }
  return (
    <TouchableOpacity onPress={onPress}>
    </TouchableOpacity>
   
  )
}

The problem arises with typescript throwing an error:

Argument of type '["RestaurantDetails", { name: string}]' is not assignable to parameter of type '[screen: "Home", params: NavigatorScreenParams<HomeStackParamList, Readonly<{ key: string; index: number; routeNames: string[]; history?: unknown[] | undefined; routes: NavigationRoute<ParamListBase, string>[]; type: string; stale: false; }>>] | [screen: ...] | [screen: ...] | [screen: ...] | [screen: ...]'.
  Type '["RestaurantDetails", { name: string}]' is not assignable to type '[screen: "Profile", params: undefined]'.
    Type at position 0 in source is not compatible with type at position 0 in target.
      Type '"RestaurantDetails"' is not assignable to type '"Profile"'.

I suspect that typescript is looking in the TRootParamList instead of HomeStackParamList for the RestaurantDetails screen. Adding RestaurantDetails to

TRootParamList</code resolves the issue but I'm unsure how to prevent this from happening.</p>
<p>Here is the code snippet from types.ts:</p>
<pre><code>import { NavigatorScreenParams } from '@react-navigation/native';

type QueueDetailsParams = {
  name: string
};

type HomeStackParamList = {
  RestaurantDetails: QueueDetailsParams | undefined;
  RestaurantList: undefined
};

type TRootParamList = {
  Home: NavigatorScreenParams<HomeStackParamList>;
  Profile: undefined
};

declare global {
  namespace ReactNavigation {
    interface RootParamList extends TRootParamList {}
  }
}

P.S. I am aware that I can update my onPress function as shown below, although I would prefer to find an alternative solution:

const onPress = () => {
    navigation.navigate("Home", {screen: "RestaurantDetails", params: {name}})
  }

Answer №1

When utilizing the useNavigation hook, it is essential to specify it with the StackNavigationProp and the parameter list that contains the desired screen:

const { moveTo } = useNavigation<StackNavigationProp<HomeStackParamList>>();

Subsequently, you can employ it on any screen within that parameter list:

moveTo('RestaurantList');

Answer №2

If bringing in navigation from props does not fix the issue, please inform me so that we can proceed with further troubleshooting steps:

const Link = ({title, url}) => {
  const onClick = () => {
    window.location.href = url;
  }
  return (
    <a href="#" onClick={onClick}>{title}</a>
  )
}

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

Schedule - the information list is not visible on the calendar

I am trying to create a timeline that loads all data and events from a datasource. I have been using a dev extreme component for this purpose, but unfortunately, the events are not displaying on the calendar. Can anyone offer any insights into what I might ...

Prevent special characters in input fields using Angular and TypeScript

I am currently working on an Angular project using Ant Design ng Zorro. I've encountered an issue with validation when trying to restrict special characters in an input field. Despite setting up the validation rules, it seems that the key press event ...

Encountered an issue in Angular 2 when attempting to add an element to an array, resulting in the error message: "Attempting to

list.component import { Component, OnInit } from '@angular/core'; import { Todo } from '../model/todo'; import { TodoDetailComponent } from './detail.component'; import { TodoService } from '../service/todo.service' ...

The new update of ag-grid, version 18.1, no longer includes the feature for enabling cell text selection

I am attempting to disable the clipboard service in ag-grid. I have come across the enableCellTextSelection flag, which supposedly disables it completely. However, when I try to use this flag as a direct property of <ag-grid-angular>, it results in a ...

Issues arise when using the react-vertical-timeline-component along with Next.js and TypeScript

Facing an issue with the react-vertical-timeline library as my library doesn't display the card with animation, showing only a line instead. The problem seems to be related to the incompatibility between the React library and my current application. ...

Clipanion is unable to fulfill requests

I followed the official Clipanion documentation for creating a CLI tool () and even cloned an example from here - https://github.com/i5ting/clipanion-test, but I'm facing issues when trying to execute my commands. It seems like I might be struggling ...

Encountered a problem while attempting to post in Angular, receiving an error message stating "net::ERR

I recently started learning Nodejs. I've created an API on a local server using Mysql and I'm working on the frontend with Angular, while using Nodejs and Express as the backend. However, I'm facing an issue where my Angular app cannot conne ...

A proposal for implementing constructor parameter properties in ECMAScript

TypeScript provides a convenient syntax for constructor parameter properties, allowing you to write code like this: constructor(a, public b, private _c) {} This is essentially shorthand for the following code: constructor(a, b, _c) { this.b = b; thi ...

What is the best way to show all cards in Angular when no filtering input is provided?

I have implemented a filter feature for multiple cards in Angular using Pipes. The filter works well, but I am facing an issue where no cards are displayed when there is no input value provided. I would like all the cards to be displayed when no input is g ...

In React Native, send parameters back to previous screen and update state

I am having trouble passing param data from the 2nd screen to the 1st screen in React Native. As a newcomer to React Native, I tried searching on Google for a solution, but it seems that the code is not working properly due to changes in the React Native v ...

The data type 'string' cannot be assigned to the type 'SystemStyleObject | undefined' in the context of Next.js and Chakra UI

I am currently working on a project that involves Next.js, TypeScript, and Chakra UI. While configuring Button themes in the button.ts file, I encountered an error related to the baseStyle object for properties like borderRadius, color, and border: Type & ...

The HTTP GET request will not be duplicated or executed until another request is made

Having trouble polling data with a GET request from the API. I want to poll data every 1 second for up to 30 seconds. The issue is, angular appears to be sending requests (logging responses), but doesn't actually send a request to the server. Here ar ...

In TypeScript, this regular expression dialect does not permit the use of category shorthand

Recently, I attempted to implement a regular expression in TypeScript: I ran the following code: const pass = /^[\pL\pM\pN_-]+$/u.test(control.value) || !control.value; To my surprise, an error occurred: "Category shorthand not allowed in ...

Adding a feature to a React project

After importing the following code snippet into a test file: https://i.sstatic.net/i44pI.png Everything seems to be working fine. However, issues arise when trying to import it into another file—here is what I'm using: https://i.sstatic.net/dsBQu ...

The Google Maps API is throwing an error because it cannot access the property 'nativeElement' of an undefined object

I'm currently using Google Maps and I want to display the map only if a certain condition is true. HTML: <div *ngIf="anyboolToShowMapOrNot" #map id="map"></div> Here's my TypeScript code: @ViewChild("map",{static: true}) mapEle ...

Oops! The program encountered an issue where it was unable to access the properties of an undefined variable, specifically while trying to

When creating a custom validation function in Angular, I encountered an issue where using a variable within the validation would result in an error: "ERROR TypeError: Cannot read properties of undefined (reading 'file')". This occurred when chang ...

Combining HTML with multiple .ts files in Angular

I've been dedicating time to enhancing an Angular application, particularly a sophisticated table with intricate styling and functionality. Within my component file, there is a whopping 2k lines of code that includes functions for text formatting, ta ...

The function was operational before, but currently it is indicating that it is no longer functioning as a

I encountered an issue with my code where a service that was previously working has suddenly stopped functioning and is now displaying an error message stating that it's not a function. Thanks to Sajeetharan for helping me out initially. constructo ...

The array containing numbers or undefined values cannot be assigned to an array containing only numbers

Currently facing an issue with TypeScript and types. I have an array of IDs obtained from checkboxes, which may also be empty. An example of values returned from the submit() function: const responseFromSubmit = { 1: { id: "1", value: "true" }, 2: ...

Exploring the Angular TypeScript Method for Rendering Nested Objects in an Array

Within this array, I have a collection of objects that contain properties for model and color. My goal is to present these objects in a table format, with individual access to the color values. grp = [ {model: "CF650-3C", color: {Orange: 3, Black ...