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) {
    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
  setState(state =>{
    count = count + 1
  return count
  setState(state =>{
    count = count - 1
  return count
  setState(state =>{
    count = 0
  return count
  // Change code above this line
  render() {
    return (
        <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>

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

ReferenceError: setState is not defined


is what console displays when i attempt to push any button

Clicking the increment button should increment the count by

Clicking the decrement button should decrement the count by
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.