Search code examples
reactjsuser-interfacesyntax-erroruser-experience

react project variable is not defined error


I was following code with mosh react tutorial suddenly I got an error of variable undefined I don't know the error. It shows the error is in line 21:55. I think the error is a silly mistake but I can't find it.


class Counter extends Component {

    state = {
        count: 0,
    };

    handleInc = prod => {
        console.log(prod);
        this.setState({ count: this.state.count + 1 });
    };



    render() {

        return (
            <div>
                <span className={this.getBadgeType()}>{this.getFormatCount()}</span>
                <button onClick={() => this.handleInc(prod)} 
                className="btn btn-secondary btn-sm"
                >
                    Increment
                </button>


            </div>
        );
    }

    getBadgeType() {
        let classes = "badge m-2 badge-"
        classes += (this.state.count === 0) ? "warning" : "primary";
        return classes;
    }

    getFormatCount() {
        return this.state.count === 0 ? 'Zero' : this.state.count;
    }
}

export default Counter;

Solution

  • It says the prod is undefined.

    The code must be modified as follows:

    <button onClick={() => this.handleInc()}
    

    This modification will work normally.