0

In this code, if I write add function like add = function(){} or add(){} , then I doesn't work. Are there some reasons?

    class App extends.React.Component{
state = {

count: 0

};
    add = () => {
        console.log(‘add’);
        this.setState({count: this.state.count + 1});
      }
      minus = () => {
        console.log(‘minus’);
        this.setState({count: this.state.count - 1});
      }
      render(){
        return <div><Potato />
        <div>class Component : {this.state.count}</div>
        <button onClick={this.add}>add</button>
        <button onClick={this.minus}>minus</button>
        </div>
      }
    }
Dain Park
  • 31
  • 4

1 Answers1

-1
class App extends React.Component {
  state = {
    count: 0,
  };
  add = () => {
    console.log("add");
    this.setState({ count: this.state.count + 1 });
  };
  minus = () => {
    console.log("minus");
    this.setState({ count: this.state.count - 1 });
  };
  render() {
    return (
      <div>
        {/* <Potato /> */}
        <div>class Component : {this.state.count}</div>
        <button onClick={this.add}>add</button>
        <button onClick={this.minus}>minus</button>
      </div>
    );
  }
}

export default App;

correct typos and added state