最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use new Feature Hooks in React? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Nov 20, 2018 at 17:11 Yangshun Tay 53.2k33 gold badges123 silver badges150 bronze badges asked Oct 26, 2018 at 12:58 RamRam 4326 silver badges23 bronze badges 1
  • As far is I know, React Hooks moved into 16.7.0-alpha.2. – Robin Wieruch Commented Dec 23, 2018 at 4:43
Add a comment  | 

1 Answer 1

Reset to default 19

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 [email protected] [email protected]

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)

发布评论

评论列表(0)

  1. 暂无评论