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

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

问题描述

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

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.

我当前正在使用版本16.6.0

I am currently using version 16.6.0

最后我明白了钩子.

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;

我导入为Fun并用于我的app.js文件

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

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

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.

谢谢

推荐答案

挂钩是16.8.0版的一部分,您可以通过安装React和React-dom 16.8.0来使用它

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

运行

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


在版本16.6.0中没有挂钩,但是挂钩是版本16.7.0的建议.但是,您可以使用React的16.7.0-alpha.0 alpha版本对其进行测试


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

要使用此版本,请使用

yarn add react@next react-dom@next

确保同时安装reactreact-dom,否则将收到类似

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

TypeError:尝试使用react时,Object(…)不是函数"错误 钩子(字母)

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

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

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