[英]Import hooks into React Typescript
I am trying to implement hooks into a React (^16.6.0) application using TypeScript 我正在尝试使用TypeScript将钩子实现到React(^ 16.6.0)应用程序中
import * as React, {useState} from 'react';
Any idea what is the right syntax for this import? 知道此导入的正确语法是什么吗?
import
supports a limited set of syntax variations. import
支持一组有限的语法变体。
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. 缺点是整个库都是导入的,因为React
是默认导出,不能摇晃。 Since the presence of React
import is needed to use JSX syntax, a more efficient way is: 由于使用JSX语法需要React
import的存在,因此一种更有效的方法是:
import * as React from 'react';
import {useState} from 'react';
Hooks were introduced in pre-release React 16.7 . 挂钩是在预发布的React 16.7中引入的 。 react
version constraint should be ^16.7.0-alpha.0
, @types/react
should be ^16.7.0
. react
版本约束应为^16.7.0-alpha.0
, @types/react
应为^16.7.0
。
I had the same error on "@types/react": "^16.8.17"
. 我在"@types/react": "^16.8.17"
上遇到了相同的错误"@types/react": "^16.8.17"
。 Looking at it's type def file, it was missing the useState
function for some reason, though it was mentioned in the comments of other hooks like useReducer
. 查看它的类型def文件,由于某种原因,它缺少useState
函数,尽管在诸如useReducer
之类的其他挂钩的注释中提到了它。
Upgrading to "@types/react": "^16.8.18"
with npm i @types/react@latest
fixed it. 使用npm i @types/react@latest
升级到"@types/react": "^16.8.18"
npm i @types/react@latest
修复了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.