React - Write a Simple Counter

Tell us what’s happening:
ok so ive tried the returns for the increment , decrement and reset in and out of the setState({}) neither works…
obviously there is a step connecting the button press to the functions
… what am i missing?

Your code so far

class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
    // Change code below this line
  this.increment = this.increment.bind(this)
  this.decrement = this.decrement.bind(this)
  this.reset = this.reset.bind(this)

    // Change code above this line
  }
  // Change code below this line
increment(){
  setState(state =>{
    count = count + 1
      })
  return count
  }
decrement(){
  setState(state =>{
    count = count - 1
  })
  return count
}
reset(){
  setState(state =>{
    count = 0
  })
  return count
}
  // Change code above this line
  render() {
    return (
      <div>
        <button className='inc' onClick={this.increment}>Increment!</button>
        <button className='dec' onClick={this.decrement}>Decrement!</button>
        <button className='reset' onClick={this.reset}>Reset</button>
        <h1>Current Count: {this.state.count}</h1>
      </div>
    );
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/114.0

Challenge: React - Write a Simple Counter

Link to the challenge:

It’s this.setState(...

This is a class, which creates an instance of a type of object (Counter in this case). The keyword this, in the code defined in the class, should refer to that instance of that object (the object has a method called setState).

Delete the returns

setState sets a value you’re storing on that object, it doesn’t return anything.


Edit: for future reference

removed em… i get what your saying there
:slight_smile:

ReferenceError: setState is not defined

:woozy_face:

is what console displays when i attempt to push any button

Clicking the increment button should increment the count by

1
Clicking the decrement button should decrement the count by
1
Clicking the reset button should reset the count to 0
. // tests completed // console output act(…) is not supported in production builds of React, and might not behave as expected.

thats what i get if i try to “pass” it

i currently have the exact same code - returns

this has something to do with how im declaring the count increase / decrease doesnt it

So have you used this.setState, because there isn’t a function defined called setState

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.