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

TypeError: Cannot destructure property 'onGestureHandlerStateChange' of 'this.ref.props' as it is undefined. #1164

Closed
hamidfzm opened this issue Aug 11, 2020 · 18 comments · Fixed by #1220
Labels
Area: Gesture handlers This issue is related to problems with gesture handlers Platform: Web

Comments

@hamidfzm
Copy link

This is a bug in react native web. I want to detect double tap using TapGestureHandler. It works perfectly fine in Android and iOS but in Web, I get this error:

TypeError: Cannot destructure property 'onGestureHandlerStateChange' of 'this.ref.props' as it is undefined.
TapGestureHandler.GestureHandler.sendEvent
node_modules/react-native-gesture-handler/web/GestureHandler.js:151
  148 | 
  149 | sendEvent = nativeEvent => {
  150 |   const {
> 151 |     onGestureHandlerStateChange: onHandlerStateChange,
      | ^  152 |     onGestureHandlerEvent: onGestureEvent,
  153 |   } = this.ref.props;
  154 | 

Versions

react-native: 0.62.2
react-native-web: 0.13.6
react-native-gesture-handler: 1.7.0

Sample code to generate issue

// Tapper.tsx
import React from 'react';
import {
  State,
  TapGestureHandler,
  TapGestureHandlerStateChangeEvent,
} from 'react-native-gesture-handler';

type Props = {
  onSingleTap?: () => void;
  onDoubleTap?: () => void;
  children: React.ReactNode;
};

const Component: React.FC<Props> = ({ children, onDoubleTap, onSingleTap }) => {
  const doubleTapRef = React.useRef<TapGestureHandler>();
  const handleSingleHandlerStateChange = React.useCallback(
    (event: TapGestureHandlerStateChangeEvent) => {
      if (event.nativeEvent.state === State.ACTIVE) {
        onSingleTap?.();
      }
    },
    [onSingleTap]
  );

  const handleDoubleHandlerStateChange = React.useCallback(
    (event: TapGestureHandlerStateChangeEvent) => {
      if (event.nativeEvent.state === State.ACTIVE) {
        onDoubleTap?.();
      }
    },
    [onDoubleTap]
  );

  return (
    <TapGestureHandler
      onHandlerStateChange={handleSingleHandlerStateChange}
      waitFor={doubleTapRef}
    >
      <TapGestureHandler
        // @ts-ignore
        ref={doubleTapRef}
        onHandlerStateChange={handleDoubleHandlerStateChange}
        numberOfTaps={2}
      >
        {children}
      </TapGestureHandler>
    </TapGestureHandler>
  );
};

Component.displayName = 'Tapper';

export default Component;

Just use this component with any component in web.

@hamidfzm hamidfzm changed the title TypeError: Cannot destructure property 'onGestureHandlerStateChange' of 'this.ref.props' as it is undefined. (web) [Web] TypeError: Cannot destructure property 'onGestureHandlerStateChange' of 'this.ref.props' as it is undefined. Aug 11, 2020
@jakub-gonet jakub-gonet added Area: Gesture handlers This issue is related to problems with gesture handlers Platform: Web labels Aug 27, 2020
@prft-alex-mattice
Copy link

Hi, I had investigated this issue for one of our projects and thought I would post my findings. I don't think we will be able to commit time to posting a PR, but I've at least diagnosed the problem. This may be of help to others and the maintainers.

We recently updated a project from React Native Web from 0.12 to 0.13, and then needed to downgrade. In the release notes, it seems that RNW has changed which props it is forwarding through its refs to the DOM. It seems that there are a few places in the react-native-gesture-handler web code where it is accessing these props through the ref.

As a temporary workaround, downgrading to 0.12 stops the crash for the time being, with the caveat that you're missing compatibility with changes from later RN versions (ex: no Pressable, if I'm not mistaken). Later this week, if it's deemed helpful, I can try to find the spots in the codebase where these references are at least occurring if it will help speed up the creation of a patch.

@emech-en
Copy link

emech-en commented Sep 8, 2020

I had the same problem.

Downgrading react-native-web to 0.12.3 solved my problem.

@janicduplessis
Copy link
Contributor

janicduplessis commented Sep 29, 2020

I was able to work the issue by making sure the children of GestureHandler components is a class that renders a View

Something like this works:

class NonForwardedRefAnimatedViewHack extends React.Component<any> {
  render() {
    return <Animated.View {...this.props} />;
  }
}

...

<TapGestureHandler>
  <NonForwardedRefAnimatedViewHack />
</TapGestureHandler>

@Shafran123
Copy link

Same issue i got

@hunght
Copy link

hunght commented Oct 18, 2020

same here i'm using expo sdk version 39.0.2,

@AlastairTaft
Copy link

@janicduplessis You legend. That avoids me having to downgrade.

@fortezhuo
Copy link

@AlastairTaft / @janicduplessis
I can't get Swipeable Component works with that trick.. Can you give me some clue ?

@difaananda40
Copy link

I'm facing this problem right now, any solution please?

@hosseinmd
Copy link
Contributor

I have the same problem.

"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.0",
"react-native-web": "^0.14.4",

@jakub-gonet jakub-gonet changed the title [Web] TypeError: Cannot destructure property 'onGestureHandlerStateChange' of 'this.ref.props' as it is undefined. TypeError: Cannot destructure property 'onGestureHandlerStateChange' of 'this.ref.props' as it is undefined. Nov 6, 2020
@keziabaidoo
Copy link

Cannot read property 'onGestureHandlerStateChange' of undefined
at NativeViewGestureHandler.GestureHandler.sendEvent

need help please

@jjborie
Copy link

jjborie commented Nov 18, 2020

@keziabaidoo , the bug has been fixed yesterday use:

"react-native-gesture-handler": "github:software-mansion/react-native-gesture-handler#196782c740cde1464e4ce4f5f46bf2c756ecdc43",

It work for me.

@keziabaidoo
Copy link

did not work
for me

@milkcha
Copy link

milkcha commented Nov 19, 2020

same problem.

@monjohn
Copy link

monjohn commented Nov 20, 2020

Latest master did not fix the problem for me, but downgrading to 12.3 did.

@chaaya
Copy link

chaaya commented Nov 26, 2020

There is a new version out there with a fix for it according to the changelog.

https://github.com/software-mansion/react-native-gesture-handler/releases/tag/1.9.0

@keziabaidoo
Copy link

keziabaidoo commented Nov 26, 2020 via email

karol-bisztyga added a commit to software-mansion/react-native-reanimated that referenced this issue Dec 10, 2020
## Description

For some time we were unable to use `react-native-web` above version `0.12.3`. Along with gesture handler `1.9.0`, this has been fixed as stated [here](software-mansion/react-native-gesture-handler#1164 (comment)).

It's required to pass a function on the web.

Fixes #1406 

## Changes

In Hooks.js instead of using `useEvent` on the web, we just return the handling function(also extract the `nativeEvent` in that case).
For example project, the package versions have been updated.

## Test code and steps to reproduce

Just run the example screens(like DragAndDrop or ChatHeads)

Co-authored-by: Jakub Gonet <[email protected]>
@kwoxer
Copy link

kwoxer commented Dec 27, 2020

I'm getting this Uncaught TypeError: can't access property "onGestureHandlerStateChange", _this$ref$props is undefined error on any of these react-native-web version:

"react-native-web": "~0.14.4",
"react-native-web": "~0.13",
"react-native-web": "~0.12",

I'm using:

@kwoxer
Copy link

kwoxer commented Dec 27, 2020

Ahh sorry, didn't see that the fix happened in react-native-gesture-handler.

Upgrading to
"react-native-gesture-handler": "~1.9.0",
fixed the issue.

flyskywhy added a commit to flyskywhy/ReactWebNative8Koa that referenced this issue May 21, 2023
…oth-slider) may cause [TypeError: Cannot destructure property 'onGestureHandlerStateChange' of 'this.ref.props' as it is undefined](software-mansion/react-native-gesture-handler#1164)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Gesture handlers This issue is related to problems with gesture handlers Platform: Web
Projects
None yet
Development

Successfully merging a pull request may close this issue.