如何解决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;
和失败
解决方法
Ciao,问题出在您使用的React版本中。挂钩是在v16.8中添加的,因此代码中的useState
和useEffect
是未定义的。不幸的是TypeError: Object(...) is not a function
并没有为您提供有用的信息。
尝试安装react 16.8并解决问题。