Error encountered while passing properties to component

I'm having trouble with a particular code snippet:

type MenuItemType = { name: string; link: string };

There's also an array of these types:

const menuItems: MenuItemType[] = [
    { name: "FAQ", link: "/faq" },
    { name: "Terms of use", link: "/terms" },
    { name: "Cookie policy", link: "/cookie-policy" },
    { name: "Privacy policy", link: "/privacy-policy" },
    { name: "Subscription", link: "/subscription" },
];

I have a component that accepts props of the same type:

function ListItem({ name, link }: MenuItemType) {
    return (
        <li className="text-xs mr-14 last:mr-0">
            <Link href={link}>
                <a>{name}</a>
            </Link>
        </li>
    );
}

The ListItem Component is used in a map function, and TypeScript is showing an error.

https://i.sstatic.net/0v4DG.png

Any ideas on what could be wrong?

Answer №1

If you have created your ListItem component to expect individual props for the name and link, rather than a combined prop for the item, you will need to use it as follows:

<ListItem name={item.name} link={item.link} />
// or:
<ListItem {...item} />

However, if you prefer to pass a single item prop instead, you can modify ListItem like this:

function ListItem({ item }: { item: MenuItemType }) {
    return (
        <li className="text-xs mr-14 last:mr-0">
            <Link href={item.link}>
                <a>{item.name}</a>
            </Link>
        </li>
    );
}

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 power of D3, TypeScript, and Angular 2

I am facing challenges incorporating D3 v4 with Angular 2 (Typescript). While exploring D3 v4, I have referred to several solutions on StackOverflow without success. I have imported most of the necessary D3 libraries and typings (utilizing TS 2.0) and adde ...

Determine the conditional type based on the type of another variable

function updateFilterData( mode: 'PaymentType' | 'Origin' | 'Destination', value: string, ) { } I need to modify this function so that when mode is 'Origin' | 'Destination', the value should b ...

Obtain user_metadata from user session middleware in NextJs with Auth0

i am attempting to retrieve the user_metadata using the useUser hook. Here is my approach. Authentication Action: exports.onExecutePostLogin = async (event, api) => { const namespace = 'https://my-tenant-auth0.com'; api.idToken.setCustomC ...

Fairly intricate React function component declaration with TypeScript

const withAuth = () => <OriginalProps extends {}>( Component: React.ComponentType<OriginalProps & IAuthContextInterface> ) => { } (withAuth()(PrivateRoute)) // this is how the HOC called Could someone simplify what this function d ...

Creating a factory function through typhography

I have a dynamically generated list of functions that take an argument and return different values: actions: [ param => ({name: param, value: 2}), param => ({label: param, quantity: 4}), ] Now I am looking to create a function that will gen ...

How to effectively handle null values using try..catch statement in typescript

As a beginner, I am learning how to write a try/catch statement in TypeScript. My issue is that there is a function within the "try" block that returns null. How can I implement code in the "catch" block specifically for when the function in "try" returns ...

The colors in React Material UI Theme provider remain consistent in version 5x and do not alter

I am developing an application in NextJs that needs to support custom themes. After going through the Material UI documentation, I tried to change the primary color by doing something like this: import { ThemeProvider, createTheme } from '@emotion/rea ...

Tips for handling a jest mock function of a module and TypeScript types

When it comes to writing my testing files in TypeScript, I rely on ts-jest and jest. I'm facing some confusion regarding how to type the mock function of a module. Let's take a look at the code: ./module.ts: import {IObj} from '../interf ...

Encountered an error of 'npm ERR! invalid semver' when attempting to release an npm package

npm ERR! code EBADSEMVER npm ERR! invalid semver: npm ERR! Check out the full log of this run here: I attempted to reinstall node and semver, but unfortunately it did not resolve the issue. ...

Creating an npm library using TypeScript model classes: A step-by-step guide

Currently, I am working on a large-scale web application that consists of multiple modules and repositories. Each module is being developed as an individual Angular project. These Angular projects have some shared UI components, services, and models which ...

Using Typescript to combine strings with the newline character

Currently, I am delving into Angular2 and facing the challenge of creating a new line for my dynamically generated string. For example: input: Hello how are you ? output: Hello how are you? Below is the code snippet: .html <div class="row"> ...

Writing for: Either attribute "a" is defined or attributes "b" and "c" are both defined

How can TypeScript understand whether this.a will be set or if this.b and this.c will be set? In the given code, it is ensured that there will never be a situation where all items are set or none are set due to how the constructor is structured. export cl ...

Tips for passing TouchableOpacity props to parent component in React Native

I created a child component with a TouchableOpacity element, and I am trying to pass props like disabled to the parent component. Child component code: import React from 'react'; import {TouchableOpacity, TouchableOpacityProps} from 'react- ...

Formatting database values in `where` conditions for strings in TypeORM: A simple guide

I am trying to utilize TypeORM's "exist" method in order to check if a name has already been inserted into the database. My issue is that I cannot determine whether the name was inserted in uppercase or lowercase, leading to potential false validatio ...

Having trouble converting the response into a valid TypeScript value for storage

These are the interfaces I have described: enum ProductType {current, closed, coming} export interface CurrentProductForCarousel { type_product:ProductType.current; offers: ProductMainInfo[] } export interface ProductMainInfo { id: number; disclai ...

implement some level of control within the ngFor directive in Angular

For instance, let's say I have an ngfor loop: <ng-container *ngFor="let setting of settings | trackBy: trackById"> <button mat-button [matMenuTriggerFor]="menu">Menu</button> <mat-me ...

Transforming the velocity template within an API Gateway to be seamlessly integrated into Typescript using AWS CDK

Currently, I'm utilizing typescript in conjunction with AWS CDK to produce a cloudFormation template for an api gateway. The process involves using an Apache Velocity template to assist in converting my response data. However, as I go about creating t ...

I'm having trouble getting this text to align properly next to the image. Plus, I can't seem to achieve a horizontal screen alignment either

There seems to be an answer available, but I'm clearly not understanding it, so I've come here seeking further explanation and assistance. CSS can be quite complex, right? Here's a screenshot of my current project: https://i.stack.imgur.com/ ...

Steps to correcting the return data type of the final promise in a chain?

Seeking a solution to return the last promise in a chain as the outcome of a function. Currently utilizing TypeScript 1.7 and native, ES6 promises. Despite attempting different approaches, I've found that TypeScript interprets the first promise (Uint ...

Can you explain the purpose of the _app.js and _document.js files in Next.js? What is the significance of using an underscore (_) in Next.js?

After using npx create-next-app to create a next.js app, I noticed that there are 2 JavaScript files named app and document in the pages directory with an initial underscore. What is the purpose of this naming convention? The files appear like this: ▼ p ...