Created
July 3, 2024 16:38
-
-
Save MarekZeman91/3a5136bfd055aa338d959a6636636a3e 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 type { Dispatch } from "react"; | |
import { useState } from "react"; | |
export function useAutoResetState<S>( | |
initialState: S | (() => S), | |
): [S, Dispatch<S>] { | |
const [state, setState] = useState(initialState); | |
return [ | |
state, | |
(temporaryState: S) => { | |
setState((prevState) => { | |
setTimeout(setState, 1, prevState); | |
return temporaryState; | |
}); | |
}, | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment