'Line 17:36: 'state' is not defined no-undef [closed]

here is my code and I cannot find where the problem is

import React, {Component} from "react";

export default class Product extends Component{

state = {
    id: this.props.product.id,
    productName: this.props.product.productName,
    price: this.props.product.price,
}
render(){
    return<div className="col-lg-g">
        <div className="card m-2">
            <div className="card-body">
                <div className="text-muted"># {this.props.id}</div>
                <h5 className="p-5 border-top">{this.props.productName}</h5>
                <div>$ {this.props.price}</div>
                //{console.log(state)}
            </div>
        </div>
        </div>
}

}

it shows Line 17:36: 'state' is not defined no-undef whenever I run the code



Solution 1:[1]

That's a class, when you write state = ... , that's an instance property, hence you access it with this.state.

Solution 2:[2]

ok i did console.log(state) but i should have used console.log(this.state)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cesare Polonara
Solution 2 Noob_Gamer