-
Notifications
You must be signed in to change notification settings - Fork 30.4k
React components with multiple root elements are not supported #20356
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
Comments
See PR #19363 |
That PR has been merged, so now does this work ? |
It throws the same error with Returning array of elements works in React 16, even from stateless functional components. The SO answer is incorrect. I can suppress the Typescript error and have my code working if I cast my component as |
Just leaving my reproduction repo of my SO question here for reference: |
Experiencing the same problem using @types/react version 16.0.18. As simple as |
The problem is still present for |
The problem is still present for 16.0.25 |
Trying to keep as much type safety as I could, I'm typing my SFC that return arrays like this atm:
|
Is this resolved yet? I am having the same issue trying to return a map of items in a SFC |
No. We'll probably need to wait for microsoft/TypeScript#21699 which is aimed to be included in TypeScript 2.8. There are several steps which need to be implemented first: microsoft/TypeScript#20239 (comment). |
Since microsoft/TypeScript#20239 (comment) has been closed now, do we still have to wait for microsoft/TypeScript#21699 to get this fixed? |
I was able to resolve this by returning my map of items inside a fragment. It's not ideal, but it works. |
To workaround the issue in the meantime I was able to suppress the error by casting the component to const Items: any = ({ items }: MyProps) => items.map(item => <div key={item.id}>{item.name}</div>) Like other provided workarounds, it's not 100% ideal but it does resolve the issue. |
(#20356 (comment))
|
This is essentially a duplicate of #18912. Once microsoft/TypeScript#21699 is resolved we should be able to use |
Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the |
@types/react
package and had problems.Definitions by:
inindex.d.ts
) so they can respond.React 16 components can have multiple root elements.
const Items = ({ items }: MyProps) => items.map(item => <div key={item.id}>{item.name}</div>)
However, TypeScript will break with:
error TS2605: JSX element type 'Element[]' is not a constructor function for JSX elements. Property 'render' is missing in type 'Element[]'.
The text was updated successfully, but these errors were encountered: