From a68befbca54ec5015a5a8b5941a2096a472031a9 Mon Sep 17 00:00:00 2001 From: Arseny <132773930+better-salmon@users.noreply.github.com> Date: Tue, 3 Mar 2026 13:09:12 +0200 Subject: [PATCH] Initialize WebHaptics instance eagerly Previously the instance was created inside `useEffect`, meaning it wasn't available until after paint. This changes make `WebHaptics` available immediately. Since the `WebHaptics` class does not use any web APIs in the constructor, it's safe to create it eagerly. Relevant React documentation: https://react.dev/reference/react/useRef#avoiding-recreating-the-ref-contents https://18.react.dev/reference/react/useRef#avoiding-recreating-the-ref-contents --- packages/web-haptics/src/react/useWebHaptics.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/web-haptics/src/react/useWebHaptics.ts b/packages/web-haptics/src/react/useWebHaptics.ts index ac0ce42..388d05e 100644 --- a/packages/web-haptics/src/react/useWebHaptics.ts +++ b/packages/web-haptics/src/react/useWebHaptics.ts @@ -11,8 +11,11 @@ import type { export function useWebHaptics(options?: WebHapticsOptions) { const instanceRef = useRef(null); - useEffect(() => { + if (instanceRef.current === null) { instanceRef.current = new WebHaptics(options); + } + + useEffect(() => { return () => { instanceRef.current?.destroy(); instanceRef.current = null;