Created
September 22, 2021 21:49
-
-
Save caasi/6c88bbfb76cce656727b612703ac62cb to your computer and use it in GitHub Desktop.
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
type SetStateAction<S> = S | (prevState: S) => S; | |
export const isStateTransition = <S>( | |
x: SetStateAction<S> | |
): x is StateTransition<S> => { | |
return typeof x === "function"; | |
}; | |
export const tapStateAction = <S>( | |
s: SetStateAction<S>, | |
f: (x: S) => void | |
): SetStateAction<S> => { | |
if (isStateTransition(s)) { | |
return (prevState: S) => { | |
const newState = s(prevState); | |
f(newState); | |
return newState; | |
}; | |
} | |
f(s); | |
return s; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment