3

I have code a List component (List.tsx) to show items, like following:

type PersonInGeneric = {
  id: number;
  first: string;
  last: string;
};

type ListProps<T> = {
  items: T[];
  onClick: (value: T) => void
};

export const List = <T extends PersonInGeneric>({ 
  items, 
  onClick, 
}: ListProps<T>) => {
  return(
    <>
      <h2>List of items</h2>
      {items.map((item, index) => {
        return(
          <div key={item.id} onClick={() => onClick(item)}>
            {item}
          </div>
        );
      })}
    </>
  );
}

And this is how I implement it in App.tsx (no error here) :

<List
        items={[
          {
            id: 1,
            first: 'Kai',
            last: 'Doe',
          }
        ]}
        onClick={item => console.log(item)}
/>

However, there is an error in List.tsx and if I hover on {item} it says :

" Type 'T' is not assignable to type 'ReactNode'. Type 'PersonInGeneric' is not assignable to type 'ReactNode'. Type 'T' is not assignable to type 'ReactPortal'. Type 'PersonInGeneric' is missing the following properties from type 'ReactPortal': key, children, type, propsts "

What is wrong with List.tsx? Thanks in advance.

1
  • Did you manage to solve this issue? I have the same. Commented Apr 6, 2023 at 16:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.