Last active
March 7, 2020 09:13
-
-
Save sanusart/a35e0f881ba42ce83de31374b0e42bca to your computer and use it in GitHub Desktop.
useListIterate hook - iterates over array of items with predefined timing #react #hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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