Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Web) setImmediate is not defined #2402

Closed
nandorojo opened this issue Feb 1, 2023 · 4 comments · Fixed by #2404
Closed

(Web) setImmediate is not defined #2402

nandorojo opened this issue Feb 1, 2023 · 4 comments · Fixed by #2404

Comments

@nandorojo
Copy link
Contributor

Description

Screenshot 2023-02-01 at 3 51 28 PM

GestureDetector breaks on Next.js for using setimmediate. This same thing was fixed on Reanimated by switching to requestAnimationFrame

Steps to reproduce

Use GestureDetector in Next.js causes this. It's an old bug from Reanimated.

The fix is here: https://github.com/software-mansion/react-native-reanimated/pull/1521/files

Snack or a link to a repository

Let me know if this is needed.

Gesture Handler version

2.9.0

React Native version

RNW 0.18

Platforms

Web

JavaScript runtime

None

Workflow

None

Architecture

None

Build type

None

Device

None

Device model

No response

Acknowledgements

Yes

@github-actions
Copy link

github-actions bot commented Feb 1, 2023

Hey! 👋

The issue doesn't seem to contain a minimal reproduction.

Could you provide a snack or a link to a GitHub repository under your username that reproduces the problem?

@nandorojo
Copy link
Contributor Author

Here's a patch for anyone who needs it:

diff --git a/node_modules/react-native-gesture-handler/lib/module/handlers/gestures/GestureDetector.js b/node_modules/react-native-gesture-handler/lib/module/handlers/gestures/GestureDetector.js
index baf1ca3..0c9f50b 100644
--- a/node_modules/react-native-gesture-handler/lib/module/handlers/gestures/GestureDetector.js
+++ b/node_modules/react-native-gesture-handler/lib/module/handlers/gestures/GestureDetector.js
@@ -23,6 +23,8 @@ import { RNRenderer } from '../../RNRenderer';
 import { isExperimentalWebImplementationEnabled } from '../../EnableExperimentalWebImplementation';
 const ALLOWED_PROPS = [...baseGestureHandlerWithMonitorProps, ...tapGestureHandlerProps, ...panGestureHandlerProps, ...panGestureHandlerCustomNativeProps, ...longPressGestureHandlerProps, ...forceTouchGestureHandlerProps, ...flingGestureHandlerProps];
 
+const scheduleUpdate = Platform.OS == 'web' ? requestAnimationFrame : setImmediate;
+
 function convertToHandlerTag(ref) {
   if (typeof ref === 'number') {
     return ref;
@@ -84,7 +86,7 @@ function attachHandlers({
   // when it's ran
 
 
-  setImmediate(() => {
+  scheduleUpdate(() => {
     if (!mountedRef.current) {
       return;
     }
@@ -100,7 +102,7 @@ function attachHandlers({
   // when it's ran
 
 
-  setImmediate(() => {
+  scheduleUpdate(() => {
     if (!mountedRef.current) {
       return;
     }
@@ -163,7 +165,7 @@ function updateHandlers(preparedGesture, gestureConfig, gesture, mountedRef) {
   // in case of external relations)
 
 
-  setImmediate(() => {
+  scheduleUpdate(() => {
     if (!mountedRef.current) {
       return;
     }

@j-piasecki
Copy link
Member

Thanks for providing the fix ❤️.

j-piasecki added a commit that referenced this issue Feb 3, 2023
## Description

Replace `setImmediate` with `requestAnimationFrame` on the web.

Closes
#2402.

## Test plan

Tested on the Example app.
@alzalabany
Copy link

alzalabany commented May 18, 2023

diff --git a/node_modules/react-native-gesture-handler/lib/module/handlers/gestures/GestureDetector.js b/node_modules/react-native-gesture-handler/lib/module/handlers/gestures/GestureDetector.js
index baf1ca3..edc3d9a 100644
--- a/node_modules/react-native-gesture-handler/lib/module/handlers/gestures/GestureDetector.js
+++ b/node_modules/react-native-gesture-handler/lib/module/handlers/gestures/GestureDetector.js
@@ -23,6 +23,8 @@ import { RNRenderer } from '../../RNRenderer';
 import { isExperimentalWebImplementationEnabled } from '../../EnableExperimentalWebImplementation';
 const ALLOWED_PROPS = [...baseGestureHandlerWithMonitorProps, ...tapGestureHandlerProps, ...panGestureHandlerProps, ...panGestureHandlerCustomNativeProps, ...longPressGestureHandlerProps, ...forceTouchGestureHandlerProps, ...flingGestureHandlerProps];
 
+const setImmediate = global.setImmediate || (fn => setTimeout(fn, 0));
+
 function convertToHandlerTag(ref) {
   if (typeof ref === 'number') {
     return ref;

this works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants