Skip to content

Instantly share code, notes, and snippets.

@spacesuitdiver
Created September 9, 2024 12:04
Show Gist options
  • Save spacesuitdiver/ebc832ebdc0b67221139cbcbdb3f0e95 to your computer and use it in GitHub Desktop.
Save spacesuitdiver/ebc832ebdc0b67221139cbcbdb3f0e95 to your computer and use it in GitHub Desktop.
import React, { ComponentType } from 'react';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import hoistNonReactStatics from 'hoist-non-react-statics';
// Helper function to get the display name of the wrapped component
const getDisplayName = (WrappedComponent: ComponentType<any>): string =>
WrappedComponent.displayName || WrappedComponent.name || 'Component';
// Create the HOC
const withGestureHandlerRootView = <P extends object>(
WrappedComponent: ComponentType<P>
) => {
// Create a new component that wraps the passed component with GestureHandlerRootView
const WithGestureHandlerRootView: React.FC<P> = (props: P) => {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<WrappedComponent {...props} />
</GestureHandlerRootView>
);
};
// Set the display name for better debugging
WithGestureHandlerRootView.displayName = `withGestureHandlerRootView(${getDisplayName(
WrappedComponent
)})`;
// Hoist non-react statics to preserve static methods
hoistNonReactStatics(WithGestureHandlerRootView, WrappedComponent);
return WithGestureHandlerRootView;
};
export default withGestureHandlerRootView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment