summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-31 19:40:23 +0200
committerArmand Philippot <git@armandphilippot.com>2022-06-01 22:32:09 +0200
commit8320b1d39ea6402c32e907dbb35082efc6af9f5a (patch)
treeb5ee9586a4ec91aa15c92dcb513b551716fd4416 /src/utils
parent994ad1bec193b2d1a6e0d38d6ef3f3d2bd66c3ea (diff)
chore: replace the toggle component
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/hooks/use-state-change.tsx19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/utils/hooks/use-state-change.tsx b/src/utils/hooks/use-state-change.tsx
new file mode 100644
index 0000000..063fb8e
--- /dev/null
+++ b/src/utils/hooks/use-state-change.tsx
@@ -0,0 +1,19 @@
+import { useEffect, useState } from 'react';
+
+/**
+ * Use React useState hook and update it if initial data change.
+ *
+ * @param initial - The initial value.
+ * @returns The state and a setter.
+ */
+const useStateChange = <T,>(initial: T) => {
+ const [state, setState] = useState<T>(initial);
+
+ useEffect(() => {
+ setState(initial);
+ }, [initial]);
+
+ return [state, setState] as const;
+};
+
+export default useStateChange;