Skip to content

Commit

Permalink
Add error about functional components being used with old API (#3240)
Browse files Browse the repository at this point in the history
## Description

Old API does not support functional components as it requires assigning `ref` to handler's child. If someone tries to pass functional component into handler, behavior differs based on platform:

- Android - app will crash with the following error message:
<img width="341" alt="image" src="https://github.com/user-attachments/assets/a274b6e3-a36f-4f7d-8d23-c9606ae32974">

- Web - app will crash in `GestureHandlerDelegate`:
  - `Cannot read properties of undefined (reading 'isButtonInConfig')`

- iOS - app won't crash, but the handler doesn't work anyway


This PR adds `error` that will be thrown when handler cannot obtain target view. This will unify behavior across platforms, as well as provide explanation of what went wrong. 

## Test plan

<details>
<summary>Tested on the following code:</summary>

```jsx
import React from 'react';
import { View } from 'react-native';
import { TapGestureHandler } from 'react-native-gesture-handler';

const MyView = () => {
  return <View />;
};

export default function RectButtonBorders() {
  return (
    <TapGestureHandler onHandlerStateChange={() => console.log('click')}>
      <MyView />
    </TapGestureHandler>
  );
}
```

</details>
  • Loading branch information
m-bert authored Nov 28, 2024
1 parent 967212e commit fe66fe1
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/handlers/createHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ export default function createHandler<
)
);

if (!this.viewNode) {
throw new Error(
`[Gesture Handler] Failed to obtain view for ${Handler.displayName}. Note that old API doesn't support functional components.`
);
}

this.attachGestureHandler(findNodeHandle(this.viewNode) as number); // TODO(TS) - check if this can be null
}

Expand Down

0 comments on commit fe66fe1

Please sign in to comment.