The React team announced that since React 16:
You can now return an array of elements from a component’s render method.
This works in typescript for regular class components, but I cannot get it to work for stateless functional components.
See this repo if you want to reproduce for yourself.
Code:
import * as React from 'react';
// See this example working in a non-TS environment: https://codesandbox.io/s/ppl3wopo8j
class Hello extends React.Component {
render() {
return <div>
<Foo />
<Bar /> {/* Error: 'JSX element type 'Element[]' is not a constructor function for JSX elements. */}
</div>;
}
}
class Foo extends React.Component {
render() {
return [<div>foo</div>, <div>fooz</div>];
}
}
function Bar() {
// Cannot render an array from a SFC
return [<div>bar</div>, <div>baz</div>];
}
Building this snipper results in the following error:
'JSX element type 'Element[]' is not a constructor function for JSX elements.
Property 'render' is missing in type 'Element[]'.'
As you can see, rendering an array from a regular class component works, but it fails when rendering an array from a stateless functional component.
I'm unsure whether the problem lies with my code/setup, the DefinitelyTyped react types, or with typescript itself.
SFC
andStatelessComponent
are deprecated in React 17. The deprecation notice suggests users useFunctionComponent
orFC
instead. – Martimartial