Why cant I access 'this' in this react code? The problem is only in the increment method. The other methods work just fine. Why?
import React, { Component } from 'react'; import ReactDOM from 'react-dom'; class Counter extends Component { state = { count: 1 }; handleIncrement() { console.log(this.state.count); } render() { return ( <div> <span className = {this.getBadgeClasses()}> {this.formatCount()}</span> <button onClick={this.handleIncrement} className = "btn btn-secondary btn-sm" > Increment </button> </div> ); } getBadgeClasses() { let classes = "badge m-2 badge-"; classes += this.state.count === 0 ? "warning" : "primary"; return classes } formatCount(){ let {count} = this.state; return count === 0 ? 'Zero' : count } } ReactDOM.render(<Counter />, document.getElementById("root"));