# How to make setInterval() work in React?

**URL:** <https://forum.freecodecamp.org/t/how-to-make-setinterval-work-in-react/325427>\
**Category:** General\
**Created:** [November 5, 2019, 1:47am UTC](https://forum.freecodecamp.org/t/how-to-make-setinterval-work-in-react/325427 "2019-11-05T01:47:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kafein](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kafein/32/125857_2.png) [@Kafein](https://forum.freecodecamp.org/u/Kafein)\
**Post date:** [November 5, 2019, 1:47am UTC](https://forum.freecodecamp.org/t/how-to-make-setinterval-work-in-react/325427/1 "2019-11-05T01:47:26Z")

</div>

So I’m trying to start the Pomodoro Clock project, but I’m having a bit of a problem. I’m trying to make a simple countdown timer just to test first how `setInterval()` behaves in React but I can’t get it to work properly. This is the code I’m trying to run:

```auto
function App() {
  
  const [seconds, setSeconds] = React.useState(10);
    
  const startTimer = () => {
    setInterval(decrement, 1000);

  }

  const decrement = () => {
    setSeconds(seconds - 1);
    console.log(seconds);
  }
  
  return (
    <div>
      <h1>{seconds}</h1> 
      <button onClick={startTimer}>START</button>
    </div>
  )
     
}

```

I’m basically trying to count down from 10 but I can’t get the state to change with `setInterval()`. I tried to put a console.log in my `decrement()` function to see how it behaves but it only displays the number 10 every 1 second, when it should be counting down from 10 every second. How do I get that to work in React?

Here’s a [Codepen link](https://codepen.io/KafeinFaita/pen/Jjjpmej) to test it out.

---

<div class="post-metadata">

**Author:** ![Marmiz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marmiz/32/74845_2.png) [@Marmiz](https://forum.freecodecamp.org/u/Marmiz)\
**Post date:** [November 5, 2019, 2:17am UTC](https://forum.freecodecamp.org/t/how-to-make-setinterval-work-in-react/325427/3 "2019-11-05T02:17:50Z")

</div>

There’s a wonderful blog post by Dan Abramov (one of the core member of React) that uses timer to better explain Hooks, I suggest you to give it a read 🙂

> **[Making setInterval Declarative with React Hooks](https://overreacted.io/making-setinterval-declarative-with-react-hooks/)**
>
> How I learned to stop worrying and love refs.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [November 5, 2019, 2:19am UTC](https://forum.freecodecamp.org/t/how-to-make-setinterval-work-in-react/325427/4 "2019-11-05T02:19:06Z")

</div>

No, that won’t work. You’re going to need to use a ref here. Helpfully, [there is a detailed guide on how to make it work](https://overreacted.io/making-setinterval-declarative-with-react-hooks/)

_(edit: snap)_

---

<div class="post-metadata">

**Author:** ![Kafein](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kafein/32/125857_2.png) [@Kafein](https://forum.freecodecamp.org/u/Kafein)\
**Post date:** [November 5, 2019, 3:09am UTC](https://forum.freecodecamp.org/t/how-to-make-setinterval-work-in-react/325427/5 "2019-11-05T03:09:56Z")

</div>

I tried using a class-based component instead of React Hooks and it seems to work as intended. I think I’ll stick with class-based for now since I haven’t studied Hooks quite well yet. Thanks for your answers. 🙂

---

<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:** [January 16, 2021, 2:46pm UTC](https://forum.freecodecamp.org/t/how-to-make-setinterval-work-in-react/325427/6 "2021-01-16T14:46:07Z")

</div>

Closed on January 16, 2021, 2:46pm UTC


