Skip to content

JSX element type 'Element[]' is not a constructor function for JSX elements #33487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
oonsamyi opened this issue Sep 18, 2019 · 3 comments
Closed

Comments

@oonsamyi
Copy link

oonsamyi commented Sep 18, 2019

TypeScript Version: 3.5.1

Search Terms:

  • JSX element type 'Element[]' is not a constructor function for JSX elements

  • array jsx elements aren't support

Code

const Component = () => ['item1', 'item2'].map((item) => <div>{item}</div>)
const Container = () => <Component />

Expected behavior:
Should be no errors. It is valid behavior. Flow support this behavior.

Actual behavior:
Typescript throw error:

JSX element type 'Element[]' is not a constructor function for JSX elements.
Type 'Element[]' is missing the following properties from type 'Element': type, props, key

Playground Link:
https://www.typescriptlang.org/play/index.html?jsx=2#code/JYWwDg9gTgLgBAKjgQwM5wEoFNkGN4BmUEIcA5FDvmQFA24QB2q8AwiZI1o-ALxwAKAJRxeAPjgBtMsBhYQARjIAacrPkAmMgF0AdCGRgBA9SBHi4AHgAmwAG5iA3qYC+lgPS2HQug2ZsmGGRgLihRQXMJS3ZwJm54dzEgA

@oonsamyi oonsamyi changed the title JSX element type 'Element[]' is not a constructor function for JSX elements. JSX element type 'Element[]' is not a constructor function for JSX elements Sep 18, 2019
@oonsamyi
Copy link
Author

This is the issue of DefinitelyTyped: DefinitelyTyped/DefinitelyTyped#20356

@ofhope
Copy link

ofhope commented Jan 30, 2020

If you wrap your mapped list in a fragment its valid.

const SuggestionList = (props: { suggestions: string[] }) => {
    const { suggestions } = props;
    const suggestionList = suggestions.map((suggest, i) => (<p key={`suggest-${i}`}>{suggest}</p>));
    return (<>{suggestionList}</>);
}

I had the same error while returning an array of elements. Including return (<>{suggestionList}</>); solved the problem. A component can only one container Element.

@MujunZ
Copy link

MujunZ commented Mar 2, 2020

If you wrap your mapped list in a fragment its valid.

const SuggestionList = (props: { suggestions: string[] }) => {
    const { suggestions } = props;
    const suggestionList = suggestions.map((suggest, i) => (<p key={`suggest-${i}`}>{suggest}</p>));
    return (<>{suggestionList}</>);
}

I had the same error while returning an array of elements. Including return (<>{suggestionList}</>); solved the problem. A component can only one container Element.

Thank you! This helped me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants