8

我正在尝试使用 TypeScript 在 React (^16.6.0) 应用程序中实现挂钩

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

知道这个导入的正确语法是什么吗?

4

2 回答 2

13

import supports a limited set of syntax variations.

It can be:

import React, {useState}  from 'react';

The downside is that entire library is imported, because React is default export and cannot be tree-shaken. Since the presence of React import is needed to use JSX syntax, a more efficient way is:

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

Hooks were introduced in pre-release React 16.7. react version constraint should be ^16.7.0-alpha.0, @types/react should be ^16.7.0.

于 2018-11-21T14:55:30.887 回答
7

我有同样的错误"@types/react": "^16.8.17"。查看它的类型 def 文件,useState由于某种原因它缺少该功能,尽管它在其他钩子(如useReducer.

升级到"@types/react": "^16.8.18"修复npm i @types/react@latest它。

于 2019-05-28T15:17:44.360 回答