user11709406
user11709406

Reputation:

Argument of type 'Element[]' is not assignable to parameter of type 'Element'

import React, { Fragment } from 'react'

const Ques = (Props:JSX.Element) => {
    return (
        <Fragment>{Props}</Fragment>
    );
}
const ListName = ["Gus","Thekla","Anna","Sophia"].map((Name)=> <div>{Name}</div>)
Ques(ListName)

in line Ques(ListName) I get this error.

Argument of type 'Element[]' is not assignable to parameter of type > > 'Element'. Type 'Element[]' is missing the following properties from type 'Element': > type, props, key

Upvotes: 4

Views: 11538

Answers (1)

Edgar
Edgar

Reputation: 6846

You are passing an array of JSX.Element,

const Ques = (Props:JSX.Element[]) => {
    return (
        <Fragment>{Props}</Fragment>
    );
}
const ListName = ["Gus","Thekla","Anna","Sophia"].map((Name)=> <div>{Name}</div>)
Ques(ListName)

When you run this code, you’ll be given a warning that a key should be provided for list items. A “key” is a special string attribute you need to include when creating lists of elements.

I would write like this

const Ques = (Props:JSX.Element[]) => {
    return (
        <Fragment>{Props}</Fragment>
    );
}
const ListName = ["Gus","Thekla","Anna","Sophia"].map((Name,nameindex)=> <div key={`${Name}${nameindex/10}`}>{Name}</div>)
Ques(ListName)

You can read about the keys here link.

Upvotes: 5

Related Questions