-
I am porting parts of export function useRecognizers<Config extends GenericOptions>(
handlers: InternalHandlers,
config: Config | {} = {},
gestureKey?: GestureKey,
nativeHandlers?: NativeHandlers
): HookReturnType<Config> {
const ctrl = React.useMemo(() => new Controller(handlers), [])
ctrl.applyHandlers(handlers, nativeHandlers)
ctrl.applyConfig(config, gestureKey)
React.useEffect(ctrl.effect.bind(ctrl))
React.useEffect(() => {
return ctrl.clean.bind(ctrl)
}, [])
// When target is undefined we return the bind function of the controller which
// returns prop handlers.
// @ts-ignore
if (config.target === undefined) {
return ctrl.bind.bind(ctrl) as any
}
return undefined as any
} My first draft looked like this: export function useRecognizers<Config extends GenericOptions>(
handlers: InternalHandlers,
config: Config | {} = {},
gestureKey?: GestureKey,
nativeHandlers?: NativeHandlers
): HookReturnType<Config> {
const ctrl = createMemo(() => new Controller(handlers))
ctrl().applyHandlers(handlers, nativeHandlers)
ctrl().applyConfig(config, gestureKey)
createEffect(ctrl().effect.bind(ctrl()))
createEffect(() => {
return ctrl().clean.bind(ctrl())
})
// When target is undefined we return the bind function of the controller which
// returns prop handlers.
// @ts-ignore
if (config.target === undefined) {
return ctrl().bind.bind(ctrl()) as any
}
return undefined as any
} I tried avoiding export function useRecognizers<Config extends GenericOptions>(
handlers: InternalHandlers,
config: Config | {} = {},
gestureKey?: GestureKey,
nativeHandlers?: NativeHandlers
): HookReturnType<Config> {
const ctrl = new Controller(handlers)
ctrl.applyHandlers(handlers, nativeHandlers)
ctrl.applyConfig(config, gestureKey)
onMount(() => {
ctrl.effect.bind(ctrl)
})
onCleanup(() => {
ctrl.clean.bind(ctrl)
})
// When target is undefined we return the bind function of the controller which
// returns prop handlers.
// @ts-ignore
if (config.target === undefined) {
return ctrl.bind.bind(ctrl) as any
}
return undefined as any
} Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yep because Solid only runs once, if there is nothing that requires reactive tracking and updates the |
Beta Was this translation helpful? Give feedback.
Yep because Solid only runs once, if there is nothing that requires reactive tracking and updates the
createMemo
is unnecessary. The empty dependency list in React is why I feel extra confident of this, since it indicates it never updates. At that point you may only needonMount
to wait until after render andonCleanup
to well, cleanup. I'm not even sureonMount
is absolutely necessary, but maybe it's better to wait to do that part of the setup until after render.