Flow: How to use flow and jsx without importing react in 0.53?

Created on 19 Aug 2017  路  14Comments  路  Source: facebook/flow

Flowtype now react-only? In 0.53 above example generates error: "identifier React. Could not resolve name"

// import React from 'react'

function A(
  props: {a: string;}
) {
  return <div>{props.a}</div>
}

<A a='123' />

try

How to use power of flowtype in jsx pure components without importing react?

Most helpful comment

Try this: declare var React: $Exports<'react'>;

All 14 comments

Flow has support for the @jsx pragma, but it doesn't work on TryFlow.

See https://github.com/facebook/flow/blob/master/newtests/jsx_pragma/test.js

Ok, this workaround works:

//@flow
// @jsx Bar

function Bar<Props>(component: ((props: Props, ...x: any[]) => any) | string, props: Props, ...childs: any[]) {}

function A(
  props: {a: string;}
) {
  return <div>{props.a}</div>
}

<A a="1" />

But how to do same without placing @jsx in each file and without importing Bar?

@zerkalica Why do you need it? It won't work without importing React anyway.

@vkurchatkin

  1. Autoimporting react babel-plugin-react-require
  2. Simple bridge to react-like jsx-compatible frameworks, like ivi, vue-jsx
  3. Framework lockin
  4. My experiments with react-free statefull flow-compatible component

Simple counter:

class Counter {
    @mem value = 0
}

export function CounterView(
    _: {},
    {counter}: {
        counter: Counter
    }
) {
    return <div>
        <div>
            Count: {counter.value}
        </div>
        <button onClick={() => { counter.value++ }}>Add</button>
        <button onClick={() => { counter.value = ('error': any) }}>Gen error</button>
    </div>
}
CounterView.deps = [{counter: Counter}]

I guess the easiest way would be do declare global React.

Do you now how to do that?

I try:

// flow-typed/react-stub.js

// @flow
import R from 'react'
declare var React: R
// test.js
//@flow

function A(
  props: {a: string;}
) {
  return <div>{props.a}</div>
}

<A a={1} />

flow does not check errors:

$flow check
Found 0 errors

Try this: declare var React: $Exports<'react'>;

It works, thank you.

@vkurchatkin's solution works great, but are there any docs for the @jsx pragma? @samwgoldman

@sobolevn, @nucleartide I recommend opening a new issue

@mrkev I wonder why it is recommended to open a new issue if this one actually was never resolved?

Recommending declare var React: $Exports<'react'>; sounds like a joke.

I found this clean way to work for me, in your .flowconfig file add react.runtime=automatic under [options].

Was this page helpful?
0 / 5 - 0 ratings