Skip to content

Instantly share code, notes, and snippets.

@bedekelly
Created August 22, 2020 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bedekelly/b6ae74d2fb047477a3694822ed60be60 to your computer and use it in GitHub Desktop.
Save bedekelly/b6ae74d2fb047477a3694822ed60be60 to your computer and use it in GitHub Desktop.
Simple sequence hook for use with step-by-step forms etc
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