15

I'm running the latest version of React and I'm getting this error enter image description here 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"
  },
11
  • What exact statement that error is about? Commented Dec 20, 2018 at 2:25
  • I edited with a screen shot of the error Commented Dec 20, 2018 at 2:28
  • I'm not sure that line can trigger such an error :-S Commented Dec 20, 2018 at 2:29
  • I console.log useState and it comes out undefined, so its not getting imported right or something Commented Dec 20, 2018 at 2:31
  • Are you sure you're running react v16.7? Commented Dec 20, 2018 at 2:34

3 Answers 3

19

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I tried the alpha versions before, but it was alpha.0, alpha.2 is now working
2

I tried to use the following in package.json but do not work.

"react": "16.7.0-alpha.2",
"react-dom": "16.7.0-alpha.2",

What worked is the following

"react": "next",
"react-dom": "next",

EDIT

React version v16.8.0 has Hooks inside. Use that.

Comments

0

edit package.json

"react": "16.7.0-alpha.2",
"react-dom": "16.7.0-alpha.2",
"react-router-dom": "4.4.0-beta.6",

and run yarn

The react released 16.7.0, but there are no react hooks. If you use '^[email protected]', remove yarn.lock, it will install [email protected]. So you must delete '^' and use '[email protected]'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.