TypeScript&React useEffect-此表达式不可调用。类型'typeof React'没有呼叫签名TS2349

时间:2020-05-19 19:35:21

标签: reactjs typescript react-hooks use-effect

在以下代码中,我收到TypeScript错误:

This expression is not callable. Type 'typeof React' has no call signatures TS2349

我需要以什么方式声明类型?感谢我在React中习惯TypeScript的帮助。

useEffect(() => {
    // subscribe to some data source
    console.log('subscribe to some data source');
    return () => {
        // unsubscribe to avoid memory leak
        console.log('this will run when the component unmounts');
    };
});

1 个答案:

答案 0 :(得分:0)

因为我按如下方式导入了React

import * as React from "react";

我需要在useEffect钩子前面加上React这样的前缀:

React.useEffect(() => {