HomeSoftware DevelopmentHow one can use setInterval() methodology inside React elements ?

How one can use setInterval() methodology inside React elements ?

The setInterval() methodology executes a perform repeatedly at a specified interval. In a React part, we will use the setInterval methodology to replace the part’s state or carry out different actions. Nevertheless, there are a number of caveats to pay attention to when utilizing the setInterval methodology in a React part: 

  • Reminiscence Leaks: If the part unmounts earlier than the interval is stopped, the callback perform will proceed to execute and trigger a reminiscence leak. To keep away from this, it is very important clear the interval when the part unmounts by calling clearInterval in a cleanup perform supplied by useEffect.
  • Timing points: If a part re-renders often, the interval could not hearth on the anticipated interval, as a result of delay between the time the interval was set and the time the part re-renders.
  • Efficiency:  When a number of elements use the setInterval methodology, it will possibly result in efficiency points

Due to this fact, in React, it’s essential to hold monitor of the lifecycle of the react elements and cease the intervals when the part unmounts. 

Allow us to perceive the best way to use the setInterval methodology inside a react part utilizing the next instance: 

Method: We are going to create a counter whose worth updates after an interval of a second utilizing the setInterval methodology. 

Implementation and Setup for Creating React Software:

Step 1: Make a venture listing, head over to the terminal, and create a react app named counter-gfg utilizing the next command:

npx create-react-app counter-gfg

After the counter-gfg app is created, change to the brand new folder counter-gfg by typing the command beneath:

cd counter-gfg

Step 2: Modify Your venture construction:

We are going to modify the folder and hold the information we’d like for this instance. Now, ensure that your file construction appears like this:

Mission Construction: 

Last Mission Listing

Step 3: Embrace the next code in your index.html file, situated within the public folder of your venture listing. 


<!DOCTYPE html>

<html lang="en">



    <meta charset="utf-8" />

    <meta identify="viewport" 

          content material="width=device-width, initial-scale=1" />

    <meta identify="theme-color" 

          content material="#000000" />

    <meta identify="description" 

          content material="Site created utilizing create-react-app" />

    <title>Utilizing setOnterval methodology in React Elements</title>




    <div id="root"></div>




Step 4: Creating the Counter part: 

  • setInterval methodology is used contained in the useEffect hook to increment the rely state variable each second (1000 milliseconds). The
  • clearInterval methodology is used contained in the useEffect cleanup perform to cease the interval when the part unmounts.
  • The useState hook initializes the rely state variable with a worth of 0. Then the useState hook is named with a perform as an argument that units up the interval and shops the returned interval ID because the interval
  • The setInterval perform takes two arguments: a callback perform to be executed repeatedly and a delay in milliseconds. Right here, the callback perform increments the rely state variable by calling the setCount perform, which updates the worth of the rely. 
  • The useEffect hook additionally returns a cleanup perform that stops the interval by calling the clearInterval perform when the part unmounts or through the rely state variable modifications. This prevents reminiscence leaks and different points that come up when utilizing setInterval in a React part.



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


const Counter = () => {

    const [count, setCount] = useState(0);


    useEffect(() => {



        const interval = setInterval(() => {

            setCount(rely + 1);

        }, 1000);



        return () => clearInterval(interval);

    }, [count]);


    return <h1>{rely}</h1>;



export default Counter;

Step 5: Add the next code within the index.js file. The index.js file serves as the principle entry level, and inside it, the App.js file is rendered on the root ID of the DOM.



import React from 'react';

import ReactDOM from 'react-dom/consumer';

import './index.css';

import App from './App';


const root = ReactDOM.createRoot(doc.getElementById('root'));



    <App />



Step to run the appliance: Run the appliance through the use of the next command:

npm begin

Output: By default, the React venture will run on port 3000. You possibly can entry it at localhost:3000 in your browser. 

The worth of the rely is displayed on the display and can be incremented each second by the setInterval perform.

Output: Utilizing setInterval inside a React part


Most Popular

Recent Comments