Parcourir la source

新增baseHook

wyling007 il y a 3 ans
Parent
commit
6c9e6758a0
1 fichiers modifiés avec 10 ajouts et 0 suppressions
  1. 10 0
      src/hooks/base/useState.ts

+ 10 - 0
src/hooks/base/useState.ts

@@ -0,0 +1,10 @@
+import { ref } from "vue";
+import type { Ref, UnwrapRef } from "vue";
+
+export type ReturnUseState<T> = [Ref<UnwrapRef<T>>, (newValue: UnwrapRef<T>) => UnwrapRef<T>];
+
+export default function useState<T>(value: T): ReturnUseState<T> {
+	const state = ref(value);
+	const setState = (newValue: UnwrapRef<T>) => (state.value = newValue);
+	return [state, setState];
+}