Created
February 1, 2023 11:45
-
-
Save developit/7c927f19b221776eb089815d54d6cf54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* useSignal, but works as a ref on DOM elements. | |
* @template T | |
* @param {T} value | |
*/ | |
export function useSignalRef(value) { | |
const ref = /** @type {Signal<T> & { current: T }} */ (useSignal(value)); | |
if (!('current' in ref)) Object.defineProperty(ref, 'current', refSignalProto); | |
return ref; | |
} | |
const refSignalProto = { | |
configurable: true, | |
get() { | |
return this.value; | |
}, | |
set(v) { | |
this.value = v; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment