Using custom properties with RouteComponentProps from react-router-dom

In my project, I have a component named Navbar that relies on the location object from history, which is defined in RouteComponentProps.

I attempted to include a custom prop in my component like this:

interface IProps{
   title?: string
}

class Navbar extends React.Component<RouteComponentProps<{}> & IProps>{

}

Although this code does not throw an error, when I try to use it like this:

<Navbar title="My Custom Page Title" />

Typescript gives me the following error:

Type '{ title: string; }' is not assignable to type '(IntrinsicAttributes & IntrinsicClassAttributes, never> | (Pick, never> & { ...; }), any, any>> & Readonly<...> & Readonly<...>) | (IntrinsicAttributes & ... 2 more ... & Readonly<...>)'. Property 'title' does not exist on type...

Initially, I tried this approach:

interface IProps extends RouteComponentProps{
   title?: string
}

However, it also resulted in a typescript error.

I am uncertain about the correct way to extend or expand RouteComponentProps.

Answer №1

Here is a possible solution for setting up withRouter:

export default withRouter<IProps, any>(Navbar);

I am currently using "any" as the second argument, although the docs suggest using StaticContext for server-side rendering. For now, using "any" seems to work fine. The important thing is that IProps must be defined as the first argument in withRouter.

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 the functionalities of Angular routing by incorporating parameters

Greetings! I have developed a straightforward Angular 12 project featuring a login page and a home page. Below is the code structure for your reference: <div class="text-center" style="width: 100%"> <h1>home</h1> & ...

typescript TypeScript interface utilizing keys from another interface

So I've got 2 TypeScript interfaces here: interface IObject1 { propOne: string, propTwo: string, ... } interface IObject2 { 'some.prefix.propOne': string, 'some.prefix.propTwo': string, ... } Is there a more concise ...

Passing public field names with typed expressions in TypeScript

I've been exploring ways to pass an array of property names (or field names) for a specific object without resorting to using what are often referred to as "magic strings" - as they can easily lead to typos! I'm essentially searching for somethin ...

Error message in TypeScript: A dynamic property name must be a valid type such as 'string', 'number', 'symbol', or 'any'

Attempting to utilize the computer property name feature in my TypeScript code: import {camelCase} from "lodash"; const camelizeKeys = (obj:any):any => { if (Array.isArray(obj)) { return obj.map(v => camelizeKeys(v)); } else if (ob ...

How can you specify the active route in Angular?

I am curious about whether it is possible to set the active route from a script instead of just from the HTML template. Let me provide an example: @Component({ template: `<input type="button" (click)="back()" value="back" ...

Learn how to enhance a Vue component by adding extra properties while having Typescript support in Vue 3

Attempting to enhance a Vue component from PrimeVue, I aim to introduce extra props while preserving the original components' typings and merging them with my new props. For example, when dealing with the Button component that requires "label" to be ...

Unable to locate a custom definition for TypeScript v3

When I am running my webpack dev server, Typescript is generating this error: ERROR in ./src/components/allowcated-resources/AllowcatedResources.tsx Module not found: Error: Can't resolve 'my-scheduler' in 'mypath\allowcated-resou ...

Require assistance with transforming a JSON string into an object

I've been struggling with converting the JSON object returned from the service to the Directory class. The issue is that the Directory class has an 'expanded' property which is not present in the JSON object. I've tried various methods ...

Unlocking the power of module augmentation in Typescript: Enhancing library models within your app domain

I currently work with two applications that share the same code base for their models. I am interested in developing and sharing a library model using inheritance in TypeScript. For instance, Pet extends Author. In my current Angular application, I need ...

Tips for safely storing data in local storage using Angular 5

How can I securely store data in local storage, such as encrypting and decrypting the local storage data to prevent manipulation by other users? If it's not possible with local storage, what are alternative methods for client-side data storage? I&apo ...

Selecting nested attributes from a class or interface: Best practices

I am looking to retrieve a specific type from the fn function. Let's take a look at the code snippet below: for more information, this is a continuation of a previous question on Stack Overflow: this question class Person { firstName: string; las ...

Challenges in Displaying Components in React with Typescript

I'm currently facing an issue where the content I am trying to render on my screen is not appearing. Although the function correctly enters the if conditional statement, as confirmed by console logging. This is the section where I have implemented th ...

How to use $$[n] in Spectron/WebdriverIO to target the nth child element instead of the selector

Attempting to utilize Spectron for testing my Electron application has been challenging. According to the documentation, in order to locate the nth child element, you can either use an nth-child selector or retrieve all children that match a selector using ...

Leveraging _.some in lodash

I'm working on a Typescript code where I need to check if any items in one array are present in another array. Although I am new to lodash, I thought of using _.some for this task. However, the code is currently returning false when I expected it to r ...

Using TypeScript to create a unique object type that is mapped with generics for each key

Suppose I have a type representing an array of two items: a list of parameters and a function with arguments corresponding to the parameters. I've created a handy function to infer the generic type for easy use. type MapParamsToFunction<A extends a ...

Populate an empty value in a key-value pair by using the subsequent value

Replace an empty value in a key-value pair with the first value of the next key. myitems = { 'items1': [{first:true, second:false}, {first:true, second:true}], 'items2': [], //should be filled with {first:true, second:false} 'it ...

Displaying a profile hover card allows users to easily discover and choose whether to follow or explore

I created a profile hover card on my website with a follow/unfollow button. However, I encountered an issue - the hover card disappears when I move away from my profile thumbnail. How can I fix this so that the hover card stays visible on mouseover and dis ...

Can you explain the contrast in index signature types between these two examples in TypeScript?

My goal is to assign either a boolean value or a boolean array value to the object attribute depending on certain props. However, an error occurs when the key value is received as a variable rather than a literal string value. I'm struggling to unde ...

Utilizing the map function in Angular while disregarding any null values

I have an array of objects in my dataset. Here's a glimpse of how it is structured: [ { "id": 1, "name": "john", "address": { "number": 42, "street": "High Street"} }, { ...

Encountering issues when trying to import const enums in a webpack project using babel

Currently, I am working on a react project that was initially created using create-react-app. However, we later ejected and made extensive modifications to the webpack configuration. My current struggle lies in importing const enums from external libraries ...