【问题标题】:React Hooks create Failure: Type Error object (...) ist not a functionReact Hooks 创建失败:Typeerror 对象(...)不是函数
【发布时间】:2020-11-30 23:21:18
【问题描述】:

我特别想使用 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?你能发布你的 package.json 吗?
  • 如果您安装了正确的 react 和 react-dom veriosn ,请重新启动 UI 服务器
  • @simbathesailor:做到了!我重新启动了服务器,它可以工作了!谢谢
  • @GiovanniEsposito:谢谢。版本是 16.13.1 但重启服务器是解决方案
  • 哦,好的。好吧,我很高兴你解决了。祝你有美好的一天:)

标签: reactjs react-hooks typeerror


【解决方案1】:

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

尝试安装react 16.8,问题应该解决了。

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 2019-04-16
    • 2015-10-28
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    相关资源
    最近更新 更多