TSLint error: The property 'params' is not found on the type 'NavigationState'

I develop in react-native using typescript. When passing parameters to a screen, I utilize the navigate function.

this.props.navigation.navigate("NextScreen", { title: "title" })

On the NextScreen component, I retrieve these params by accessing

this.props.navigation.state.params.title
, but I encounter a tslint error regarding params.

TS2339:Property 'params' does not exist on type 'NavigationState'.

This snippet demonstrates some of the code:

import { NavigationInjectedProps } from "react-navigation";
interface OwnProps {
}
interface OwnState {
}

type Props = NavigationInjectedProps & OwnProps;

class NextScreen extends React.Component<Props, OwnState> {
    ...
    public render() {
        // The linting error occurs with this line.
        const { title } = this.props.navigation.state.params;
        ...
    }
}

I understand that defining the types for passed props is recommended, so what would be the best approach to do so?

Answer №1

I found a solution using this approach, although I'm uncertain if it's the most efficient method. After examining the original code, I redefined navigation with a custom type of

NavigationScreenProp<NavStateParams>
.

import { NavigationInjectedProps, NavigationScreenProp, NavigationState } from "react-navigation";
...
interface ParamType {
  title: string;
}
interface StateParams extends NavigationState {
  params: ParamType;
}
interface OwnProps {
  navigation: NavigationScreenProp<StateParams>;
}

type Props = OwnProps & NavigationInjectedProps;

Answer №2

Retrieve parameters in this manner

const {arguments} = this.props.navigation.state;

const heading = arguments.title

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

Exploring alternative methods for accessing object values in TypeScript using a string object path without relying on the eval function

If we have an object in JS/typescript structured like this: var obj = { a: { b:{ c:1 } } } And a string "b.c" is given, how can we evaluate this using only the variables provided and retrieve the value 1 from the object without rel ...

What would cause the nsfw property to be absent from a TextChannel in client.on("messageCreate") event?

Currently working with Typescript in combination with Discord.js v14, I'm encountering the following error: Property 'nsfw' does not exist on type 'DMChannel | PartialDMChannel | ... Below is the snippet of problematic code: client.on( ...

Encountering TypeError when trying to sign in with Google in React Native through Expo

I've been working on developing a React Native app that includes Google OAuth Login functionality. I've set up the necessary credentials in Google and added them to my app.js file. However, when I try to log in with Google on the Android emulato ...

How can I incorporate my custom component into an Angular 12 modal using NGZorro?

I want to incorporate my own component into an nzmodal. I attempted the following: <nz-modal [(nzVisible)]="isVisible" nzTitle="Create relation" (nzOnCancel)="handleCancel()" (nzOnOk)="handleOk()"> & ...

Discovering various kinds of data with a single generic type

I am looking to define a specific type like this: type RenderItems<T> = { [K in keyof T]: { label: string; options: { defaultValue: T[K]['options'][current_index_of_array]; item: (value: T[K][&apo ...

The 'path' property is not found on the 'ValidationError' type when using express-validator version 7.0.1

When utilizing express-validator 7.0.1, I encounter an issue trying to access the path field. The error message indicates that "Property 'path' does not exist on type 'ValidationError'.: import express, { Request, Response } from " ...

Remove the user along with all of their associated documents from Firestore

When a user wants to delete their account, I need to ensure that the 'documents' they created in Firebase are deleted as well. After some research online, I came across the following code snippet: deleteAccount() { const qry: firebase. ...

Error: Trying to access a property that is not declared on an empty object

Using a fully patched Visual Studio 2013, I am integrating JQuery, JQueryUI, JSRender, and TypeScript into my project. However, I am encountering an error in the ts file: Property 'fadeDiv' does not exist on type '{}'. While I believ ...

Is there a way to stop WhatsApp Web from launching in a new tab if another WhatsApp Web tab is already open?

How can I prevent the WhatsApp Web URL from opening in a new tab if there is already a WhatsApp Web tab open? const msg = "*Greetings From "+this.previewData.unit_name+" "+this.previewData.unit_location+"* "; const full = `$ ...

Handle and manage errors within the controller in Express.js to prevent the further execution of asynchronous functions

Consider a scenario where there is an API endpoint /register, designed to register new users in an application. The function utilized is asynchronous, as an attempt is made to incorporate an asynchronous process within an AuthController when performing pas ...

Organize by a collection of strings or a collection of enums

Here is a list of objects that I have: enum MealType { Breakfast, Lunch, Dinner } interface FoodItem { name: string, type: MealType[], } const foodItems: FoodItem[] = [ { name: 'Pizza', type: [MealType.Lunch, MealType.Dinner ...

The Typescript error message states that the type '{ onClick: () => void; }' cannot be assigned to the type 'IntrinsicAttributes'

I'm a beginner in Typescript and I'm encountering difficulties comprehending why my code isn't functioning properly. My goal is to create a carousel image gallery using React and Typescript. However, I'm facing issues when attempting t ...

Can you explain the distinction between needing ts-node and ts-node/register?

Currently, I am conducting end-to-end tests for an Angular application using Protractor and TypeScript. As I was setting up the environment, I came across the requirement to include: require("ts-node/register") Given my limited experience with Node.js, I ...

What is the best way to have an icon appear when a child component div is clicked, without it displaying on other similar divs?

Within my child component div, I have configured it to display information from an object located in the parent component. Initially, when the web app loads, it correctly shows three divs with names and messages retrieved from the created object. However, ...

Is it considered poor practice in TypeScript to manually set the type when the type inference is already accurate?

Is it necessary to explicitly set the variable type in TypeScript when it is inferred correctly? For example: const add = (a: number, b: number) => a + b; const result = add(2, 3); // Or should I explicitly declare the return value type? const add = ...

Creating an object key using a passed literal argument in TypeScript

Can the following scenario be achieved? Given an argument, how can we identify the object key and access it? Any potential solutions? async function checkKey(arg:'key1'|'key2'){ // fetchResult returns an object with either {key1:&apo ...

`How to prevent Query parameters from being lost upon reloading in Nextjs Router while maintaining a clean URL structure?`

My challenge lies in sending data via router.push to a page with dynamic room id (src/pages/editor/[roomid].tsx) in Next.js. I want the URL to stay clean so users can easily edit their username in the URL if needed. When initially loaded, router.query suc ...

Executes the function in the child component only if the specified condition evaluates to true

When a specific variable is true, I need to call a function in a child component. If the variable is false, nothing should happen. allowDeleteItem = false; <ChildComponent .... removeItemFn={ deleteFn } /> I attempted to use the boolean variable wi ...

What is the best way to integrate the sharing of app invites directly to WhatsApp within the app itself?

Looking for a way to invite new users to my app directly from within the app by providing them with a link to download from the Play Store through Whatsapp. (Share on Whatsapp Functionality) The issue: I'm trying to figure out how to share both an im ...

Issue encountered while deploying Node.js server on Render platform

I'm currently facing an issue while attempting to deploy a TypeScript server with Node.js to Render. I've provided the directory structure and deployment information below. Any insights or opinions you can offer would be greatly appreciated. Than ...