The 'Icon' JSX component does not contain any construction or call signatures

I am currently learning typescript and facing an issue while working on a sidebar navigation feature. I have an array containing menu items with icons that I need to display.

Here is the array:

const menuItems = [
{ id: 1, label: "Dashboard", icon: <ComputerDesktopIcon />, link: "/" },
{ id: 2, label: "Page 2", icon: <AcademicCapIcon />, link: "" },
{ id: 3, label: "Settings", icon: <Cog6ToothIcon />, link: "/" },
];

The error in my code is coming from this part:

<div className="flex flex-col items-start mt-24">
{menuItems.map(({ icon: Icon, ...menu }) => {
const classes = getNavItemClasses(menu);
return <div className={classes}>
<Link href={menu.link}>
<div className="flex py-2 px-3 items-center w-full h-full">
<div>
<Icon />
</div>
{!toggleCollapse && (
<span className={classNames('text-md font-medium text-dark-gray')}>
{menu.label}
</span>
)}
</div>
</Link>
</div>
})}
</div>

The specific problem lies in the <Icon /> call which throws the error:

JSX element type 'Icon' does not have any construct or call signatures
.

It's worth mentioning that everything functions correctly when I comment out that line.

I have gone through creating props components and other issues on Stack Overflow. However, I'm struggling with the menuItems.map section as most resources only cover defining and implementing single const items.

Answer №1

When you write the following code:

{ id: 1, label: "Dashboard", icon: <ComputerDesktopIcon />, link: "/" },

You are instantiating the React element right then and there, at the moment you declare the object. The icon property now holds a React element instead of a functional component written as a function or a class component represented in class form. Using <SomeComponentName /> only makes sense if SomeComponentName refers to a valid component or class.

The property inside the object should simply be the function or class - it can be initialized with <Icon /> later on.

const menuItems = [
    { id: 1, label: "Dashboard", icon: ComputerDesktopIcon, link: "/" },
    { id: 2, label: "Page 2", icon: AcademicCapIcon, link: "" },
    { id: 3, label: "Settings", icon: Cog6ToothIcon, link: "/" },
];

Answer №2

Error: JSX element type 'Icon' does not have any construct or call signatures

icon is actually a pre-existing react component that outputs JSX.

To fix this issue, simply swap out the self-closing tag </> with curly braces {}.

{menuItems.map(({ icon, ...menu }) => {
   ...
   {icon}
   ...
})}

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

Utilize the power of TypeScript to display data with impact

Recently, I have been working on a project in TypeScript where I need to fetch data from a URL and display it in my component. Transitioning from JavaScript to TypeScript has been a bit challenging for me. Here is the code snippet I have been working on: i ...

Is it true that Angular 4 routers only maintain a single state during navigation?

After transitioning from state A to state B in my application, I noticed that when navigating back to state A, it seems to reload. Does this mean that State A is being destroyed during the transition to state B? If so, how can I prevent State A from relo ...

the undefined 'pipe' cannot be read

Trying to perform unit testing for an Angular component is a new experience for me. Currently, I am encountering a specific issue that I would like assistance with. The component in question contains the following select statement: this.store.select(getI ...

Tips for concealing JavaScript files from the Chrome Developer Tools while using Next.js

Currently working on a project using Next.js. I've noticed that the utils/components are quite visible through the Chrome Developer Tools, as shown in this image: https://i.sstatic.net/jaLmg.png Is there a way to hide them from being easily accessib ...

Tips for incorporating attributes into a customized Material-UI props component using TypeScript in React

I'm interested in using material-ui with react and typescript. I want to pass properties to the components, but I'm having trouble figuring out how to do it. Currently, I'm working with the react-typescript example from the material-UI repos ...

The gltf file does not appear to be displaying properly within a React Next.js environment

I'm facing an issue while attempting to load a gltf file in a nextjs application using threejs. The process doesn't seem to work when running it within a nextjs application on a react project. Here's the configuration I used for next.js with ...

Registering a function for chart.js plugins that manipulates external data

Is there a way to pass external data to the chart.plugins.register function? I'm struggling because I can't access the necessary context: Chart.plugins.register( { beforeDraw: function (chart) { //implementation } }); I attempted using ...

The div is repositioned without the use of padding or margin properties

I am facing an issue with my Next.JS application where a div is not aligning properly at the top of the page. Despite setting padding and margin to 0 for body, HTML, and the div itself, it seems to be slightly off. I have checked all components thoroughly ...

What is the best way to retrieve the name of a static method within a class?

In my code, I am logging multiple messages in a static method and I want to use the method name as context. However, I do not want to create a separate variable called `context` and assign the function/method name to it. I would like to be able to access ...

Tips for managing a stored data reservoir with react-query

I am looking to develop a unique custom hook that can effectively manage a cache and fetch only new items. Here is the expected behavior: Upon initial request for [1, 2, 3, 4, 5], it should fetch all [1, 2, 3, 4, 5] as the cache is empty. If a request is ...

What is the best way to merge multiple module.exports in Next.js' next.config.js file?

I'm facing an issue in my next.config.js file where I need to export two module exports but cannot do so without combining them. Is there a way to merge the two module.exports into one? const withPWA = require("next-pwa"); module.exports = ...

A guide on setting up a countdown timer in Angular 4 for a daily recurring event with the help of Rxjs Observable and the Async Pipe

I'm currently working on developing a countdown timer for a daily recurring event using Angular 4, RxJS Observables, and the Async Pipe feature. Let's take a look at my component implementation: interface Time { hours: number; minutes: numbe ...

Troubleshooting Angular testing using Jest: Problem encountered with triggerEventHandler

I'm currently working on an Angular application and testing it with JEST. Everything seems to be running smoothly until I encounter an issue with event interactions in Angular. The test below works as expected: it('should delegate click to comp ...

Mean value calculated for each hour within a given array

Every minute, my array updates. To show the daily average of each hour, I need to calculate the average for every 60 elements. The latest minute gets added at the end of the array. // Retrieving the last elements from the array var hours = (this. ...

Setting Up Unique Error Messages using React Hook Form and Zod in Next.js

I am currently working on a Next.js web application that includes a form for buying credits. The form utilizes React Hook Form and Zod for schema validation. To view the form, check out this image. Below is the code snippet for the form: "use client ...

Discovering the parameter unions in Typescript has revolutionized the way

My current interface features overloaded functions in a specific format: export interface IEvents { method(): boolean; on(name: 'eventName1', listener: (obj: SomeType) => void): void; on(name: 'eventName2', listener: (obj: Som ...

Tips for sending class objects with Typescript/React

I need to establish an Array of items with different characteristics as a Prop on my component. My goal is to transmit an array of items with unique properties to a component. Here's an example of what my array of objects might look like: ` let dish ...

I'm sorry, we couldn't locate the module: Unable to find the path '../types/index'

After spending an hour attempting to troubleshoot this issue, I am still unable to find a solution. I have stored index.d.ts in the types folder. The content of the types file is as follows: export interface tag { created_at: string id: nu ...

Exploring type delegation in TypeScript

Here is a scenario where I am using the builder pattern in my code: export class ValidationBuilderBase implements IValidationBuilder { public isRequired(): IValidationBuilder { const validationResult = Validators.required(this.baseControl); ...

Tips for capturing an event from a bespoke button component integrated within an ng2-smart-table

Currently, my task involves triggering an event in Angular2 by clicking a button within a child component that is displayed within a ng2-smart-table located in the parent component as a column. Unfortunately, I am facing the challenge that I cannot add a ...