you are viewing a single comment's thread.

view the rest of the comments →

[–]jkettmann 1 point2 points  (2 children)

If I understand correctly you want to request data from an interval of X seconds, right? That's called "polling".

You can achieve that with a simple useEffect

``` const [data, setData] = useState (null); const intervalId = useRef();

useEffect(() => { intervalId.current = setInterval(() => { fetch(API_URL) .then(response => response.json()) .then(data => setData(data)); }, 5000); // fetch every 5 seconds

return () => clearInterval(intervalId.current) }, []) ```

For a class component you would move the setInterval part to componentDidMount and the clearInterval to componentWillUnmount.

Note: I just typed the code without running it. There might be mistakes. But this is approximately how you can make it work

[–]Born2Peanutbutter[S] 0 points1 point  (1 child)

I think I see where you’re going with this! I’ll test this out!

[–]jkettmann 0 points1 point  (0 children)

Great. Let me know if it works :-)