Created
April 20, 2021 11:40
-
-
Save matsuby/defd3e1ccc1969972a664fa61de22627 to your computer and use it in GitHub Desktop.
useStep.ts
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 { useState } from 'react' | |
export const useStep = < | |
T extends { | |
[step: string]: string | |
} | |
>({ | |
steps, | |
initialStep, | |
}: { | |
steps: T | |
initialStep: T[keyof T] | |
}) => { | |
const [step, setStep] = useState(initialStep) | |
const stepTo = Object.keys(steps).reduce( | |
(acc, k) => ({ | |
...acc, | |
[k]: () => setStep(k as T[keyof T]), | |
}), | |
{ reset: () => setStep(initialStep) } | |
) as { [step in keyof T | 'reset']: () => void } | |
return { step, stepTo } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment