Skip to content

Instantly share code, notes, and snippets.

@matsuby
Created April 20, 2021 11:40
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 matsuby/defd3e1ccc1969972a664fa61de22627 to your computer and use it in GitHub Desktop.
Save matsuby/defd3e1ccc1969972a664fa61de22627 to your computer and use it in GitHub Desktop.
useStep.ts
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