Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
Refresh data of react app from backend every 2 seconds

How do I refresh my react app every 2 seconds?

import React, {useState, useEffect} from 'react';
import {Route, Switch, useLocation} from 'react-router-dom';
import Home from './Home';

function App() {
    const location = useLocation();
    console.log(location);
    const [feedbacks, setFeedbacks] = useState([]);

    useEffect(() => {
        const fetchArticles = async () => {
            setFeedbacks([]);

            const url = 'https://url.abc.com/';
            const api_response = await fetch(url);
            let data = await api_response.json();

            setFeedbacks(data);
        }

        fetchArticles();
    }, [])

    const data = {feedbacks};

    return (
        <div className="App">
            <Switch>
                <Route path="/" render={() => <Home data={data}/>} exact/>
                <Route path="/:username" render={() => <Home data={data}/>} exact/>
            </Switch>
        </div>
    );
}

export default App;
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Have you tried using setInterval(); on fetchArticles? Something like:

setInterval(fetchArticles(), 2000)
about 4 years ago · Juan Pablo Isaza Report

0

There is a JS function for intervals "setInterval" run the function you pass in a regular interval in ms.

This should work:

const fetchArticles = async () => {
    setFeedbacks([]);
    const url = 'https://url.abc.com/';
    const api_response = await fetch(url);
    let data = await api_response.json();
    setFeedbacks(data);
}

useEffect(() => {
    setInterval(fetchArticles(), 2000)
}, [])
about 4 years ago · Juan Pablo Isaza Report

0

 useEffect(() => {

 const fetchArticles = async () => {
   setFeedbacks([]);
   const url = 'https://url.abc.com/';
   const api_response = await fetch(url);
   let data = await api_response.json();
   setFeedbacks(data);
 }

       setInterval(fetchArticles(), 2000)
 }, [])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!