Skip to content

Instantly share code, notes, and snippets.

@prdxs
Last active April 21, 2021 07:31
Show Gist options
  • Save prdxs/4d884ea7b5a461969c4a8c1507a1d76f to your computer and use it in GitHub Desktop.
Save prdxs/4d884ea7b5a461969c4a8c1507a1d76f to your computer and use it in GitHub Desktop.
export default function useData(initData) {
const [data, setData] = useState(initData);
useEffect(() => {
const intervalId = setInterval(async () => {
const newData = await fetch('https://data-api.com/getData').then(response => response.json());
setData(newData);
}, 50000);
return () => {
clearInterval(intervalId);
};
}, []);
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment