flow icon indicating copy to clipboard operation
flow copied to clipboard

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

Open zerkalica opened this issue 9 years ago • 14 comments

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?

zerkalica avatar Aug 19 '17 09:08 zerkalica

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

samwgoldman avatar Aug 19 '17 18:08 samwgoldman

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 avatar Aug 19 '17 19:08 zerkalica

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

vkurchatkin avatar Aug 19 '17 20:08 vkurchatkin

@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}]

zerkalica avatar Aug 19 '17 21:08 zerkalica

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

vkurchatkin avatar Aug 19 '17 21:08 vkurchatkin

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

zerkalica avatar Aug 20 '17 10:08 zerkalica

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

vkurchatkin avatar Aug 20 '17 18:08 vkurchatkin

It works, thank you.

zerkalica avatar Aug 20 '17 21:08 zerkalica

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

nucleartide avatar Jan 23 '18 16:01 nucleartide

I was using this trick to work with flow and vue. https://github.com/wemake-services/wemake-vue-template/blob/c203e751f9d9ed9cfc411a4cdf56d35fe8a2dda7/template/client/components/ActionBar.vue#L3

Since 0.76 it is not working any more. https://github.com/wemake-services/wemake-vue-template/pull/645

Are there any ideas how to fix it now?

sobolevn avatar Jul 11 '18 06:07 sobolevn

@sobolevn, @nucleartide I recommend opening a new issue

mrkev avatar Jul 12 '18 00:07 mrkev

Still no solution https://github.com/facebook/flow/issues/6626

sobolevn avatar Sep 07 '18 18:09 sobolevn

@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.

ArmorDarks avatar Sep 14 '18 19:09 ArmorDarks

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

CarmelK avatar Nov 30 '20 21:11 CarmelK