Skip to content

Instantly share code, notes, and snippets.

@MarekZeman91
Created July 3, 2024 16:38
Show Gist options
  • Save MarekZeman91/3a5136bfd055aa338d959a6636636a3e to your computer and use it in GitHub Desktop.
Save MarekZeman91/3a5136bfd055aa338d959a6636636a3e to your computer and use it in GitHub Desktop.
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