Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm learning React from this channel. Recently, I stumbled upon React Hooks from here. So, I tried to convert a class based component to hook based. Here is my class based component:

import React, { Component } from 'react';

class AddNinja extends Component {
    state = {
        name: null,
        age: null,
        skill: null,
    }
    handleChange = e => {
        this.setState({
            [e.target.id]: e.target.value,
        })
    }
    handleSubmit = e => {
        e.preventDefault();
        this.props.addNinja(this.state);
    }
    render() {
        return (
            <div>
                <form onSubmit={ this.handleSubmit }>
                    <label htmlFor="name">Name: </label>
                    <input type="text" id="name" onChange={ this.handleChange } />

                    <label htmlFor="age">Age: </label>
                    <input type="number" id="age" onChange={ this.handleChange } />

                    <label htmlFor="skill">Skill: </label>
                    <input type="text" id="skill" onChange={ this.handleChange } />

                    <button>Submit</button>
                </form>
            </div>
        )
    }
}

Here is my converted component: https://codesandbox.io/s/n0lw4wo550?module=%2Fsrc%2FAddNinja.js

But I'm getting following error:

enter image description here

like image 511
Mahmudul Haque Avatar asked Nov 01 '18 13:11

Mahmudul Haque


People also ask

Is not a function TypeError is not a function?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

How do you fix uncaught type error in React?

The React. js "Uncaught TypeError: X is not a function" occurs when we try to call a value that is not a function as a function, e.g. calling the props object instead of a function. To solve the error, console. log the value you are calling and make sure it is a function.

How do I update React to latest version?

To update your React version, install the latest versions of the react and react-dom packages by running npm install react@latest react-dom@latest . If you use create-react-app , also update the version of react-scripts . Copied! The command will update the versions of the react-related packages.

How do I check React version?

To check which React version is your project using you need to open the package. json. Take a look under the dependencies section. It should list all of the dependencies of your project and one of those should be React.


1 Answers

React hooks are available in React v16.8.0. updated your react and react dom version to 16.8.0.

"react": "16.8.0",
"react-dom": "16.8.0",  

Here is your code with updated verion:https://codesandbox.io/s/qq90900xr4

like image 167
Murli Prajapati Avatar answered Sep 20 '22 18:09

Murli Prajapati