React JS: Trigger a function with setInterval() to execute every second

I am trying to understand how this setInterval() works, and how to use it to trigger a function to execute every second. As an example, I try to generate a new random number every second. i.e.: the function getNewNr should be executed every second. (Later, this might be a more complex function…)

This is what I got:

import React, { useState, useEffect } from 'react'

function Random() {

    const [number, setNumber] = useState(0)

    const getNewNr = () => {
        setNumber(Math.floor(Math.random() * 100))
    }
    useEffect(() => {
        const interval = setInterval(() => getNewNr, 1000)
        return () => clearInterval(interval)
    }, [])

    return (
        <div>
            {number}
        </div>
    )
}

export default Random

But the number stays at 0 and doesn’t change. Can anyone spot the mistake?

Answer

import React, { useState, useEffect } from "react";

function Random() {
  const [number, setNumber] = useState(0);

  const getNewNr = () => {
    setNumber(Math.floor(Math.random() * 100));
  };
  useEffect(() => {
    const interval = setInterval(() => getNewNr(), 1000);
    return () => clearInterval(interval);
  }, []);

  return <div>{number}</div>;
}

export default Random;