如何在 React 中使用新的 Feature Hooks? [英] How to use new Feature Hooks in React?

查看:19
本文介绍了如何在 React 中使用新的 Feature Hooks?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了 React 的新功能钩子.阅读钩子但我无法使用它.它给了我错误.

我目前使用的是 16.6.0 版

我终于明白了钩子.

import React, {useState} from 'react';const Fun = () =>{const [count, setCount] = useState(0);返回 (<div><p>您点击了 {count} 次</p><button onClick={() =>setCount(count + 1)}>点击我

);}导出默认乐趣;

我作为 Fun 导入并在我的 app.js 文件中使用

我犯的错误是我没有安装 React v16.7.0-alpha 所以我使用 npm add react@next react-dom@next 安装.

谢谢

解决方案

Hooks 作为 16.8.0 版本的一部分发布,您可以通过安装 React 和 React-dom 16.8.0 来使用它

运行

yarn install react@16.8.0 react-dom@16.8.0

安装.为了升级反应到最新版本

yarn upgrade react react-dom

<小时>

Hooks 在 16.6.0 版本中不存在,但在 16.7.0 版本中是一个提议.但是你可以使用 16.7.0-alpha.0 alpha 版本的 React 来测试它们

为了使用这个安装以上版本使用

yarn 添加 react@next react-dom@next

确保同时安装 reactreact-dom 否则你会收到类似

的警告<块引用>

TypeError: Object(...) is not a function" 尝试使用 react 时的错误钩子 (alpha)

I just read about the react's new feature hooks.Read about hooks but i can't able to use it. it gives me error.

I am currently using version 16.6.0

Finally i got Understand the hooks.

import React, {useState} from 'react';

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

    return (
        <div>
        <p>You clicked {count} times</p>
        <button onClick={() => setCount(count + 1)}>
          Click me
        </button>
      </div>
    );}

 export default Fun;

I imported as Fun and used as in my app.js file

The mistake i made is i did't install React v16.7.0-alpha so i installed using npm add react@next react-dom@next.

Thank you

解决方案

EDIT:

Hooks are released as a part of version 16.8.0 and you can use it by installing React and React-dom 16.8.0

run

yarn install react@16.8.0 react-dom@16.8.0

to install. In order to upgrade react to latest version

yarn upgrade react react-dom


Hooks aren't present in version 16.6.0, but are a proposal for version 16.7.0. You can however use 16.7.0-alpha.0 alpha version of React to test them

In order to use this install the above version using

yarn add react@next react-dom@next

Make sure that you install both react and react-dom or else your would get warning like

TypeError: Object(…) is not a function" error when trying to use react hooks (alpha)

这篇关于如何在 React 中使用新的 Feature Hooks?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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