Definitelytyped: [@types/react] fragments in react 16

Created on 3 Oct 2017  路  10Comments  路  Source: DefinitelyTyped/DefinitelyTyped

I think you should consider to update these lines of code in order to support react 16 fragments

render(): JSX.Element | null | false | string | JSX.Element[]; //class Component<P, S>
interface ElementClass extends React.Component<any> {
    render(): Element | null | false | string | Element[];
}

https://reactjs.org/blog/2017/09/26/react-v16.0.html

Most helpful comment

This issue should be extended to also cover the improvements made to Fragments in React 16.2 that was recently released.

With 16.2 you can return <></> and <React.Fragment></React.Fragment>, none of which currently is supported.

All 10 comments

There is an open PR for this #19363

Current workaround is to use public render(): any { } on the components.

Yep! you are right I forgot to mention this workaround . Thanks a lot!

I'm still having issues with a functional component like this, any idea?

function MyComponent() {
    return [<div>1</div>, <div>2</div>];
}

The :any workaround works, but I would like to avoid it as I have noImplicitAny:false

https://stackoverflow.com/questions/47481482/react-16-fragment-functional-component-and-typescript-ts2605/47481524#47481524

This issue should be extended to also cover the improvements made to Fragments in React 16.2 that was recently released.

With 16.2 you can return <></> and <React.Fragment></React.Fragment>, none of which currently is supported.

How about this?

const Fragment = (React as any).Fragment;

For anyone wanting to avoid an extra const, I'm currently doing this:

declare module 'react' {
  class Fragment extends React.Component {}
}

so <React.Fragment> can be used 馃檪

Hi All,
something maybe changed in supporting this?

Just stumbled upon this linked in my codebase. Looks like the issue is fixed now per the comment above and my personal test.

@hecof Might be good to close this.

This is essentially a duplicate of #18912. Once Microsoft/TypeScript#21699 is resolved we should be able to use ReactNode as a valid return type for function components which includes string and boolean.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tyv picture tyv  路  3Comments

fasatrix picture fasatrix  路  3Comments

JWT
svipas picture svipas  路  3Comments

ArtemZag picture ArtemZag  路  3Comments

csharpner picture csharpner  路  3Comments