Something went wrong. Try again.
temporarily write into a Vue computed ref
Something went wrong. Try again.
504 B · 17 lines · Created 1y ago
TypeScript
123456789101112131415161718import { computed, type Ref, shallowRef } from "vue";
export const writable = <T>(upstream: () => T) => { // it's a bit weird, but this essentially guarantees that the ref gets reset // immediately as expected. this also gets around the double-calling that // using `watch` would cause. const computable = computed(() => shallowRef(upstream()));
return computed<T>({ get() { return computable.value.value; }, set(next) { computable.value.value = next; }, });};