GabVlado
GabVlado

Reputation: 75

Typescript React List

React Typescript. I want to render this list, but doesn´t render.

I'm new with Typescript , i don´t undesrtand really well how to pass props from the App.tsx to the children Technology.tsx

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;

Upvotes: 1

Views: 3790

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 187044

The props <Technology> accepts and the props you are passing in don't match up.

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

This expects props like:

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

You have to pass id and name as props explicitly.


Or you could change Technology's props to this:

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

And now you can pass in a single technology props like you have it above:

<Technology technology={item} />

Upvotes: 3

Related Questions