# React - what's the problem with my code?

**URL:** <https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577>\
**Category:** General\
**Created:** [May 28, 2020, 4:12pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577 "2020-05-28T16:12:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcoPolo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marcopolo/32/177577_2.png) [@MarcoPolo](https://forum.freecodecamp.org/u/MarcoPolo)\
**Post date:** [May 28, 2020, 4:12pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/1 "2020-05-28T16:12:13Z")

</div>

```auto
export default class App extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        counter: 25,
      }
      this.handleClickTest = this.handleClickTest.bind(this);
    }
    handleClickTest() {
        for(let i=25;i>=0;i--){
            this.setState({counter: this.state.counter-1});
        }
    }

    render() {
        return (<div> {this.state.counter}
            <form>
                <button onClick={this.handleClickTest}>start</button>
            </form>
          </div>
        );
    }
}

```

I trimmed to code to the bare minimum for your convenience. When I click ‘start’ the counts changes to 24 and nothing happens more. Why does it not continue counting down?  
EDIT: it seems to me after the counter displays 24 the page “refreshes”.

---

<div class="post-metadata">

**Author:** ![AmaliaTadevosyan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amaliatadevosyan/32/143672_2.png) [@AmaliaTadevosyan](https://forum.freecodecamp.org/u/AmaliaTadevosyan)\
**Post date:** [May 28, 2020, 4:40pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/2 "2020-05-28T16:40:01Z")

</div>

Try to remove this from this.state.counter.  
this.setState({counter: state.counter - 1})

---

<div class="post-metadata">

**Author:** ![MarcoPolo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marcopolo/32/177577_2.png) [@MarcoPolo](https://forum.freecodecamp.org/u/MarcoPolo)\
**Post date:** [May 28, 2020, 4:45pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/3 "2020-05-28T16:45:01Z")

</div>

```auto
'state' is not defined no-undef

```

I tried. Doesn’t compile.

---

<div class="post-metadata">

**Author:** ![MarcoPolo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marcopolo/32/177577_2.png) [@MarcoPolo](https://forum.freecodecamp.org/u/MarcoPolo)\
**Post date:** [May 28, 2020, 5:05pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/5 "2020-05-28T17:05:26Z")

</div>

Removing the form element makes it count down to 24 without refreshing. I still don’t understand what’s wrong with using the setState method the way I did since it’s almost the same as in the EDX React course example…

---

<div class="post-metadata">

**Author:** ![AmaliaTadevosyan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amaliatadevosyan/32/143672_2.png) [@AmaliaTadevosyan](https://forum.freecodecamp.org/u/AmaliaTadevosyan)\
**Post date:** [May 28, 2020, 5:11pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/6 "2020-05-28T17:11:55Z")

</div>

```auto
 handleClickTest(e) {
    e.preventDefault();
    this.setState((state) => ({
      counter: state.counter + 1,
    }));
  }

I tried this code and it works.

```

---

<div class="post-metadata">

**Author:** ![Bam92](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bam92/32/128056_2.png) [@Bam92](https://forum.freecodecamp.org/u/Bam92)\
**Post date:** [May 28, 2020, 5:15pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/7 "2020-05-28T17:15:17Z")

</div>

Just add this in your handleClickTest fx `e.preventDefault()` to avoid the page to refresh.

```auto
handleClickTest = (e) => {
    e.preventDefault()
      for(let i=25;i>=0;i--){
          this.setState({counter: this.state.counter-1});
      }
  }

```

---

<div class="post-metadata">

**Author:** ![Annestezia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/annestezia/32/74275_2.png) [@Annestezia](https://forum.freecodecamp.org/u/Annestezia)\
**Post date:** [May 28, 2020, 5:24pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/9 "2020-05-28T17:24:30Z")

</div>

What’s with `</View>` ?  
I believe it should be `<View/>`…

---

<div class="post-metadata">

**Author:** ![Bam92](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bam92/32/128056_2.png) [@Bam92](https://forum.freecodecamp.org/u/Bam92)\
**Post date:** [May 28, 2020, 5:27pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/10 "2020-05-28T17:27:44Z")

</div>

I agree with you. Maybe a typo?

---

<div class="post-metadata">

**Author:** ![MarcoPolo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marcopolo/32/177577_2.png) [@MarcoPolo](https://forum.freecodecamp.org/u/MarcoPolo)\
**Post date:** [May 28, 2020, 5:31pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/11 "2020-05-28T17:31:14Z")

</div>

Amalia,  
Your code was the only one that worked. [Bam92](http://forum.freecodecamp.org/u/Bam92)’s code only counted to 24. Removing the ‘this’ gave an error. I don’t understand why your code worked without and why would using an anonymous function make a difference.

---

<div class="post-metadata">

**Author:** ![Bam92](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bam92/32/128056_2.png) [@Bam92](https://forum.freecodecamp.org/u/Bam92)\
**Post date:** [May 28, 2020, 5:37pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/12 "2020-05-28T17:37:34Z")

</div>

It is working well on my side. Just make sure you write it well (arrow function).

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:51pm UTC](https://forum.freecodecamp.org/t/react-whats-the-problem-with-my-code/395577/14 "2021-06-01T18:51:48Z")

</div>

Closed on June 1, 2021, 6:51pm UTC


