Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow errors on <React.Fragment> or <></>, but not <Fragment>

I am using react v16.3.0 and flow-bin v0.69.0

Using react Fragments with either <React.Fragment> or the shorthand <></> syntax like so

import React from 'react'

const ComponentA = () => (
  <React.Fragment>
    <div>Component</div>
    <div>A</div>
  </React.Fragment>
)

const ComponentB = () => (
  <>
    <div>Component</div>
    <div>B</div>
  </>
)

Flow complains with the following error (it's an indentical error for both, just showing output for ComponentA here)

Cannot get React.Fragment because property Fragment is missing in object type [1].

  24│ const ComponentA = () => (
  25│   <React.Fragment>
  26│     <div>Component</div>
  27│     <div>A</div>
  28│   </React.Fragment>

 /private/tmp/flow/flowlib_2349df3a/react.js
 251│   declare export default {|
 252│     +DOM: typeof DOM,
 253│     +PropTypes: typeof PropTypes,
 254│     +version: typeof version,
 255│     +initializeTouchEvents: typeof initializeTouchEvents,
 256│     +checkPropTypes: typeof checkPropTypes,
 257│     +createClass: typeof createClass,
 258│     +createElement: typeof createElement,
 259│     +cloneElement: typeof cloneElement,
 260│     +createFactory: typeof createFactory,
 261│     +isValidElement: typeof isValidElement,
 262│     +Component: typeof Component,
 263│     +PureComponent: typeof PureComponent,
 264│     +Children: typeof Children,
 265│   |};

With an explicit import of Fragment, however, flow does not complain.

import { Fragment, default as React } from 'react'

const ComponentC = () => (
  <Fragment>
    <div>Component</div>
    <div>C</div>
  </Fragment>
)

What is going on here? I would like to use the <></> Fragment shorthand syntax, and this issue is stopping me from doing that for now.

When I dig into the react.js lib def referenced in the error it does appear that the error is factually correct - the export of Fragment is defined and Fragment is not defined as a property on the default export.

But the flow docs state that flow has support for react Fragments from v0.59 onwards.

So is this actually a gap in support that still exists? Or am I doing something wrong? Perhaps I somehow have an outdated lib def or have things configured wrong? I can't find anything googling for the error message, which leads me to suspect it's an issue with my setup. Also I can't quite believe that this wouldn't work out the box.

like image 455
davnicwil Avatar asked Mar 16 '18 12:03

davnicwil


1 Answers

The fix to include React.Fragment in the definition is included in this commit: https://github.com/facebook/flow/commit/b76ad725177284681d483247e89739c292ed982b

It should be available in flow 0.71

like image 180
Delapouite Avatar answered Sep 19 '22 06:09

Delapouite