React Hooks创建失败:类型错误对象...ist不是函数

问题描述

我想特别使用React钩子,以便能够使用useContext钩子。

由于这没有用,所以我详细研究了一下,发现React钩子都没有用,失败总是一样的。我关注了多个帖子,特别是将所有内容更新为最新版本。尽管如此:失败仍然存在。

这是代码

import React,{ useState,useEffect } from 'react';

// if I mark useState and/ or useEffect as comment everything works. 
// If one or both are active I get the error "TypeError: Object (...) ist not a function"
// I have updated and use react@16.13.1   react-dom@16.13.1

function Test() {

  //const [count,setCount] = useState(0); // this time useState is set as comment
  
  useEffect(() => {
    console.log("I am in useEffect")
  });
  
  console.log("I am in Test")
  
  return (
    <div>
      <p>Use Effect has worked</p>
    </div>
  );
}

export default Test; 

和失败

Here the "original" failure.

解决方法

Ciao,问题出在您使用的React版本中。挂钩是在v16.8中添加的,因此代码中的useStateuseEffect是未定义的。不幸的是TypeError: Object(...) is not a function并没有为您提供有用的信息。

尝试安装react 16.8并解决问题。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...