React: flow with import * as React from 'react'

Created on 21 Aug 2019  路  2Comments  路  Source: facebook/react

I use flow, so

import * as React from 'react',

But also I use React Hook, like useState.
so how can I import this?
like

import * as React, {useState} from 'react' 

or

import * as React from 'react';
import  {useState} from 'react';

or some other method?

Hope for better workaround!

Most helpful comment

You can't import * as React, {useState} from 'react' use an alias to import the whole react and then another entity of it in the same module import.
You can either do:

import * as React from 'react;

and then you can access the useState API within your function like this: React.useState(...)
or

import * as React from 'react';
import {useState} from 'react';

I suggest the former as it's cleaner and better when it comes to modules resolution.

Maybe take a look at this as well: JavaScript Modules

All 2 comments

You can't import * as React, {useState} from 'react' use an alias to import the whole react and then another entity of it in the same module import.
You can either do:

import * as React from 'react;

and then you can access the useState API within your function like this: React.useState(...)
or

import * as React from 'react';
import {useState} from 'react';

I suggest the former as it's cleaner and better when it comes to modules resolution.

Maybe take a look at this as well: JavaScript Modules

Yeah. Eventually the plan is to change the JSX transform to remove the need for import * altogether.

Was this page helpful?
0 / 5 - 0 ratings