React 16.7 中的 React Hooks 问题 TypeError: Object(...) is not a function [英] React Hooks issues in React 16.7 TypeError: Object(...) is not a function

查看:148
本文介绍了React 16.7 中的 React Hooks 问题 TypeError: Object(...) is not a function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行最新版本的 React,但出现此错误我有一个使用 React Hooks 的简单组件,你可以在这里看到:

import React, { useState } from "react";const AppFunction = () =>{const [count, setCount] = useState(0);const incrementCount = () =>{设置计数(计数 + 1);};返回 (<div><h1>计数:{count} </h1><button onClick={incrementCount}>点击我</button>

);};导出默认的 AppFunction;

我在堆栈溢出中发现的所有内容都说要升级库,但我有最新版本 (16.7.0) 并且尝试了 alpha 版本但没有成功,我做错了什么?

package.json

依赖项":{反应":^ 16.7.0","react-dom": "^16.7.0",反应脚本":2.1.1"},

解决方案

UPDATE

Hooks 现在作为 React v16.8.0 的一部分发布.你可以通过升级你的 React 版本来使用钩子

有关 API 的更多详细信息,请参阅文档

<小时>

React 16.7.0 不包含钩子.

根据 React 博客

<块引用>

我们的最新版本包括一个重要的性能错误修复反应迟钝.尽管没有 API 更改,但我们将其作为次要而不是补丁.

为了在您的代码中运行钩子,请参考 如何在 React 中使用新的功能钩子?

I'm running the latest version of React and I'm getting this error I have a simple Component using React Hooks as you can see here :

import React, { useState } from "react";

const AppFunction = () => {
  const [count, setCount] = useState(0);

  const incrementCount = () => {
    setCount(count + 1);
  };
  return (
    <div>
      <h1>Count:{count} </h1>
      <button onClick={incrementCount}>Click Me</button>
    </div>
  );
};

export default AppFunction;

Everything i've found about it on stack overflow says to upgrade the libraries but I have the latest version (16.7.0) and have tried the alpha version with no luck , what am i doing wrong?

package.json

"dependencies": {
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-scripts": "2.1.1"
  },

解决方案

UPDATE

Hooks are now release as part of React v16.8.0. You can use hooks by upgrading your react version

See the docs for more details on the APIs


React 16.7.0 Doesn't contain hooks.

As per the React blog

Our latest release includes an important performance bugfix for React.lazy. Although there are no API changes, we’re releasing it as a minor instead of a patch.

In order to run hooks in your code, refer How to use new Feature Hooks in React?

这篇关于React 16.7 中的 React Hooks 问题 TypeError: Object(...) is not a function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆