UNPKG

@npio/internals

Version:

A free visual website editor, powered with your own SolidJS components.

21 lines (18 loc) 534 B
import { getRequestEvent } from "solid-js/web"; export const useRequestRef = function <T>( name: string, defaultValue?: () => T, ): { current: T } { const event = getRequestEvent() as any; const refs: Record<string, { current: any }> = (event.locals._refs = event.locals._refs || {}); if (!refs[name]) { refs[name] = { current: defaultValue?.(), }; } return refs[name]; }; export const defineRequestFunction = <T>(name: string, fn: () => T) => { return () => useRequestRef<T>(name, fn).current; };