Zeyad Etman
Zeyad Etman

Reputation: 2540

TypeError: Object(...) is not a function Reactjs 16.6.3

this is my first reactjs app using hooks, i'm facing an error

TypeError: Object(...) is not a function

, my code: index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';
import Test from './Test';

ReactDOM.render(<Test />, document.getElementById('root'));

serviceWorker.unregister();

Test.jsx

import React, {useState} from 'react';

export default function Test(props) {
  const [name, ChangeName] = useState('Zeyad');

  return (
    <div>
      <input
        value={name}
        onChange={(e) => ChangeName(e.target.value)}
      />
    </div>
  )
}

here's the full error: enter image description here

I don't know what's wrong in my code?

Upvotes: 2

Views: 9606

Answers (2)

Einar &#211;lafsson
Einar &#211;lafsson

Reputation: 3177

useState is not available in Reactjs v16.6.3, you will have to upgrade to the newest version of React, or at least to React v16.8.0

reactjs.org/docs/hooks-reference.html

Upvotes: 8

KhaledMohamedP
KhaledMohamedP

Reputation: 5522

If you encounter this issue simply upgrade to the latest version of react that supports hooks. Note, you need both library react and react-dom

As of now this will work,

npm install [email protected]
npm install [email protected]

Please make sure you have the correct version, here is the is available versions list for react & react-dom

Read more about this feature in this intro here

Upvotes: 1

Related Questions