Created
August 9, 2021 14:24
-
-
Save karol-majewski/62a3e4bb54562ade9b5cff219fcc6a71 to your computer and use it in GitHub Desktop.
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
import React from 'react'; | |
import { autobind } from '@typed-decorators/autobind'; | |
class Rolodex<T> { | |
index: number; | |
length: number; | |
constructor(private sequence: NonEmptyArray<T>) { | |
this.sequence = sequence; | |
this.index = 0; | |
this.length = sequence.length; | |
} | |
@autobind | |
next(): T { | |
return this.sequence[((++this.index % this.length) + this.length) % this.length]!; | |
} | |
} | |
export function useRolodex<T>(initialSequence: NonEmptyArray<T>): Rolodex<T> { | |
const slider = React.useRef(new Rolodex(initialSequence)); | |
React.useDebugValue(slider); | |
return slider.current; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment