Skip to content

Instantly share code, notes, and snippets.

@MeetMartin
Created September 11, 2021 01:25
Show Gist options
  • Save MeetMartin/1650ad36014f1a4912cdd64e48aa6427 to your computer and use it in GitHub Desktop.
Save MeetMartin/1650ad36014f1a4912cdd64e48aa6427 to your computer and use it in GitHub Desktop.
import React from 'react';
import useInterval from '../hooks/useInterval';
const MyIntervalComponent = () => {
useInterval(() => console.log('Hello World'))(5000);
return (<p>Output 'Hello World' to console every five seconds.</p>);
};
export default MyIntervalComponent;
import { useEffect } from 'react';
const useInterval = fn => time => {
return useEffect(() => {
const myInterval = setInterval(fn, time);
return () => clearInterval(myInterval);
}, []);
};
export default useInterval;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment