List of React components created using Typescript

Struggling with rendering a list in React using Typescript.

I'm new to Typescript and unsure how to properly pass props from App.tsx to Technology.tsx

  • Vue JS
  • Node JS
  • Angular JS
  • React JS

Technology.tsx

import React from 'react';

export type State = {
    id: number;
    name:string;
  } 
   
const Technology: React.FC <State[]>  = ({id,name}) => {
    return ( 
        <div >
            <h2 key={id}>{name}</h2>
        </div>
     );
}

export default Technology; 

App.tsx

import React,{Fragment, useState} from 'react';
import Technology from './components/Technology'

 export type State = {
  id: number;
  name:string;
} 
 
const App: React.FC = () => {

  const [items, setItems] = useState  <State[]>([
    {id:2 , name: ' Vue JS' },
    {id:3 , name: ' Node JS'},
    {id:4 , name: ' Angular JS'},
    {id:1 , name: ' React JS'}
  ])
    
  return (
    <Fragment>
        <h1>List of technologies</h1>
       {items.map(item=>(
             <Technology
               technology= {item}  
            />
        ))} 
    </Fragment>
  );
}

export default App;

Answer №1

The props <Technology> is expecting and the props you are passing in do not align.

export type Info = {
  id: number;
  name:string;
} 
const Technology: React.FC <Info[]>  = ({id,name}) => { ... }

This component expects props like:

<Technology id={item.id} name={item.name} />

You need to pass id and name as props explicitly.


Alternatively, you could modify Technology's props definition to this:

export type Info = {
  technology: {
    id: number;
    name:string;
  } 
}
const Technology: React.FC <Info[]>  = ({technology}) => {
  return ( 
    <div >
      <h2 key={technology.id}>{technology.name}</h2>
    </div>
  );
}

Now, you can pass a single technology prop like you did above:

<Technology technology={item} />

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 a callback function that includes the choice of an additional second

Concern I am seeking a basic function that can receive a callback with either 1 or 2 arguments. If a callback with only 1 argument is provided, the function should automatically generate the second argument internally. If a callback with 2 arguments is s ...

Creating a TypeScript shell command that can be installed globally and used portably

I am looking to create a command-line tool using TypeScript that can be accessed in the system's $PATH once installed. Here are my criteria: I should be able to run and test it from the project directory (e.g., yarn command, npm run command) It must ...

Encountering a type error with React Navigation 5 while transitioning between stack screens

I have encountered some TypeScript errors while using react-navigation 5. It seems that I might be making mistakes in how I am typing things or structuring the app, but I'm uncertain about the exact issue. I started off by following these guides from ...

Problem with Custom MUI fields not detecting value change

I want to make custom form components so I don't have to update each item individually if I want to change the style. The ReportSelect component doesn't update the value when a menu item is selected, and the ReportTextField only works for the fir ...

I'm unable to modify the text within my child component - what's the reason behind this limitation?

I created a Single File Component to display something, here is the code <template> <el-link type="primary" @click="test()" >{{this.contentShow}}</el-link> </template> <script lang="ts"> imp ...

Why am I unable to use a string as the src in next/image component?

After importing the Image module with the code import Image from "next/image";, I encountered an error that states: The type '{ src: string; }' cannot be assigned to type 'IntrinsicAttributes & ImageProps'. The type &apo ...

Implementing express-openid-connect in a TypeScript project

Trying to incorporate the express-openid-connect library for authentication backend setup with a "simple configuration," an issue arises when attempting to access the oidc object from express.Request: app.get("/", (req: express.Request, res: express.Respon ...

Error: setPosition function only accepts values of type LatLng or LatLngLiteral. The property 'lat' must be a numerical value in agm-core agm-overlay

Currently, I am utilizing Angular Maps powered by Google @agm-core and agm-overlay to implement custom markers. However, when using the (boundsChange) function on the agm-map component, an error occurs with the message "invalidValueError: setPosition: not ...

At first, Typescript generics make an inference but are ultimately specified

In my TypeScript code, I have defined a custom Logger class with specific options. The DefaultLevel type is created as a union of 'info' and 'error'. The LoggerOptions interface includes two generics, CustomLevels and Level, where Custo ...

The data type does not match the expected type 'GetVerificationKey' in the context of express-jwt when using auth0

I am in the process of implementing auth0 as described here, using a combination of express-jwt and jwks-rsa. However, I encountered an error like the one below and it's causing issues with finishing tsc properly. Error:(102, 5) TS2322: Type 'S ...

Exploring TypeScript Compiler Options for ensuring code compliance beyond the confines of strict mode

Our goal is to set up TypeScript Compiler (TSC) with a command line option that can identify errors when developers declare class fields using implicit type expressions instead of explicit ones as illustrated below. class Appliance { //Desired coding ...

Why does the server attempt to load the chart in Angular 10 Universal using Amcharts 4?

I have experience with Angular, but I am now delving into the world of using Universal for SEO purposes. My goal is to integrate a map from amcharts 4, which works fine without Angular Universal. However, I am facing an issue where the server attempts to ...

Issue: Catching errors in proxy function calls

I am currently using Vue 3 along with the latest Quasar Framework. To simplify my API calls, I created an Api class as a wrapper for Axios with various methods such as get, post, etc. Now, I need to intercept these method calls. In order to achieve this ...

Why am I encountering numerous errors while attempting to install Juice Shop?

My attempt to install the juice shop app from GitHub resulted in 63 errors showing up after running the command npm install. [riki@anarchy]: ~/juiceShop/juice-shop>$ npm install (Various warnings and engine compatibility issues) Multiple vulnerabilit ...

swap the keys and values of a record type in a literal form

How can I reverse the keys and values of a record literal in typescript? For example: type Foo = { x: "a", y: "b", z: "c" }; I want to create a type Flip<X> where: type Bar = Flip<Foo>; // should result in { a: & ...

merging pictures using angular 4

Is there a way in Angular to merge two images together, like combining images 1 and 2 to create image 3 as shown in this demonstration? View the demo image ...

Ways to reset an input field when focused

Looking to implement a number input field in React that clears the initial value when the user clicks on it. While there are solutions available for text inputs, I have not come across a method for number inputs. Every attempt I make at solving this issu ...

Unable to locate the story details with the ID 167 and publisher ID 84 within the search state

Unable to find the solution for story.detail?storyId=167&pubId=84 in state search var params = 'storyId='+$scope.stories[index].ID+'&pubId='+$scope.pubId; $state.go('story.detail?'+params); $stateProvider .state( ...

Is there a workaround for utilizing reducer dispatch outside of a React component without relying on the store?

Recently, I implemented a reducer in my project that involves using react, typescript and nextJS. I am wondering if there is a method to trigger the reducer outside of a react component, such as from an API service. While searching for solutions, most re ...

Creating a TypeScript interface that has keys determined by the elements in an array

My goal is to create a function that returns a record with keys specified by a string array. For example: // return type -> { itemA:SomeType,itemB:SomeType } const res = doThing(['itemA', 'itemB']) Do you think this is achievable? ...