From 8320b1d39ea6402c32e907dbb35082efc6af9f5a Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Tue, 31 May 2022 19:40:23 +0200
Subject: chore: replace the toggle component

---
 src/utils/hooks/use-state-change.tsx | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 src/utils/hooks/use-state-change.tsx

(limited to 'src/utils/hooks/use-state-change.tsx')

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;
-- 
cgit v1.2.3

