You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
18 lines
475 B
18 lines
475 B
import type { Ref } from 'vue';
|
|
import { ref } from 'vue';
|
|
|
|
export default function useState<T, R = Ref<T>>(
|
|
defaultStateValue?: T | (() => T),
|
|
): [R, (val: T) => void] {
|
|
const initValue: T =
|
|
typeof defaultStateValue === 'function' ? (defaultStateValue as any)() : defaultStateValue;
|
|
|
|
const innerValue = ref(initValue) as Ref<T>;
|
|
|
|
function triggerChange(newValue: T) {
|
|
innerValue.value = newValue;
|
|
}
|
|
|
|
return [innerValue as unknown as R, triggerChange];
|
|
}
|