Created
August 22, 2020 15:09
-
-
Save bedekelly/b6ae74d2fb047477a3694822ed60be60 to your computer and use it in GitHub Desktop.
Simple sequence hook for use with step-by-step forms etc
This file contains hidden or 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
function useSequence({ values, onComplete }) { | |
const [index, setIndex] = useState(0); | |
function next() { | |
setIndex((oldIndex) => { | |
if (oldIndex + 1 >= values.length) { | |
setImmediate(onComplete); | |
return oldIndex; | |
} else { | |
return oldIndex + 1; | |
} | |
}); | |
} | |
return [values[index], next]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment