Skip to content

Instantly share code, notes, and snippets.

@sanusart
Last active March 7, 2020 09:13
Show Gist options
  • Save sanusart/a35e0f881ba42ce83de31374b0e42bca to your computer and use it in GitHub Desktop.
Save sanusart/a35e0f881ba42ce83de31374b0e42bca to your computer and use it in GitHub Desktop.
useListIterate hook - iterates over array of items with predefined timing #react #hooks
import React, { useState, useEffect } from 'react';
export default function useListIterate(items, interval = 3000) {
const [item, setItem] = useState(items[0]);
useEffect(() => {
let index = 0;
const timer = setInterval(() => {
setItem(items[index]);
index++;
(index >= items.length) && (index = 0);
}, interval);
return () => clearInterval(timer);
}, []);
return item;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment