Skip to content

Instantly share code, notes, and snippets.

@Shilo
Last active November 11, 2025 05:04
Show Gist options
  • Select an option

  • Save Shilo/62e74e920232efabe3d6bf6e3893b99c to your computer and use it in GitHub Desktop.

Select an option

Save Shilo/62e74e920232efabe3d6bf6e3893b99c to your computer and use it in GitHub Desktop.
Svelte 5 ref wrapper for universal reactivity. (Not sure if it's useful)
export function ref(initialValue) {
const state = $state({value: initialValue});
return state;
}
export function watchedRef(initialValue, onSet, onGet) {
let value = $state(initialValue)
return {
set value(newValue) {
const oldValue = value
value = newValue
onSet?.(value, oldValue)
},
get value() {
onGet?.(value)
return value
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment