You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
how to wrap Snackbar component with Reanimated?
#4476
Invariant Violation: Looks like you're passing a function component Snackbar to createAnimatedComponent function which supports only class components. Please wrap your function component with React.forwardRef() or use a class component instead.
Attempt 1 (Animated.createAnimatedComponent(Snackbar)):
Reanimated's createAnimatedComponent requires direct access to an underlying native host component (like a native View) to set native props and animate styles directly on the UI thread without going across the bridge. In react-native-paper, Snackbar is a composite functional component that does not expose a native ref via React.forwardRef. When passed to createAnimatedComponent, Reanimated detects that it is a plain function component without ref forwarding and throws the invariant violation.
Attempt 2 (React.forwardRef wrapping <Snackbar>):
Creating a wrapper component with React.forwardRef and passing ref={ref} to <Snackbar> fails because Snackbar itself doesn't accept or forward ref to its root native view. React warns with "Function components cannot be given refs", the ref remains unresolved (undefined/null), and Reanimated cannot attach to the native node.
Solution: Wrap with an Animated.View and GestureDetector
Instead of wrapping Snackbar with createAnimatedComponent, the standard pattern is to wrap <Snackbar> in an <Animated.View> controlled by GestureDetector from react-native-gesture-handler.
Key Detail for React Native Paper:
By default, Snackbar has an internal style of { position: 'absolute', bottom: 0, width: '100%' }. To allow your outer <Animated.View> to control the positioning and gesture translations cleanly, pass wrapperStyle={{ position: 'relative', bottom: 0 }} to <Snackbar>.
Reproducible Code Example
Here is a drop-in SwipeableSnackbar component that supports swiping left, right, or down to dismiss using Reanimated 3 and Gesture Handler:
importReact,{useEffect}from'react';import{Dimensions,StyleSheet}from'react-native';import{Snackbar,SnackbarProps}from'react-native-paper';import{Gesture,GestureDetector}from'react-native-gesture-handler';importAnimated,{useSharedValue,useAnimatedStyle,withTiming,withSpring,runOnJS,}from'react-native-reanimated';const{width: SCREEN_WIDTH}=Dimensions.get('window');constSWIPE_THRESHOLD_X=SCREEN_WIDTH*0.35;// 35% screen widthconstSWIPE_THRESHOLD_Y=50;// 50px downwardconstVELOCITY_THRESHOLD=600;typeSwipeableSnackbarProps=SnackbarProps;exportconstSwipeableSnackbar: React.FC<SwipeableSnackbarProps>=({
visible,
onDismiss,
children,
action,
duration,
wrapperStyle,
...rest})=>{consttranslateX=useSharedValue(0);consttranslateY=useSharedValue(0);// Reset positions when snackbar is reopeneduseEffect(()=>{if(visible){translateX.value=0;translateY.value=0;}},[visible,translateX,translateY]);constpanGesture=Gesture.Pan().onUpdate((event)=>{translateX.value=event.translationX;// Clamp Y to only allow downward drag (positive values)translateY.value=Math.max(0,event.translationY);}).onEnd((event)=>{constswipedRight=translateX.value>SWIPE_THRESHOLD_X||event.velocityX>VELOCITY_THRESHOLD;constswipedLeft=translateX.value<-SWIPE_THRESHOLD_X||event.velocityX<-VELOCITY_THRESHOLD;constswipedDown=translateY.value>SWIPE_THRESHOLD_Y||event.velocityY>VELOCITY_THRESHOLD;if(swipedRight){translateX.value=withTiming(SCREEN_WIDTH,{duration: 180},()=>{runOnJS(onDismiss)();});}elseif(swipedLeft){translateX.value=withTiming(-SCREEN_WIDTH,{duration: 180},()=>{runOnJS(onDismiss)();});}elseif(swipedDown){translateY.value=withTiming(150,{duration: 180},()=>{runOnJS(onDismiss)();});}else{// Snap back if threshold wasn't reachedtranslateX.value=withSpring(0,{damping: 15});translateY.value=withSpring(0,{damping: 15});}});constanimatedStyle=useAnimatedStyle(()=>({transform: [{translateX: translateX.value},{translateY: translateY.value},],}));if(!visible)returnnull;return(<GestureDetectorgesture={panGesture}><Animated.Viewstyle={[styles.outerWrapper,animatedStyle,wrapperStyle]}><Snackbarvisible={visible}onDismiss={onDismiss}action={action}duration={duration}// Neutralize default internal absolute positioningwrapperStyle={styles.innerWrapper}{...rest}>{children}</Snackbar></Animated.View></GestureDetector>);};conststyles=StyleSheet.create({outerWrapper: {position: 'absolute',bottom: 0,left: 0,right: 0,zIndex: 9999,},innerWrapper: {position: 'relative',bottom: 0,},});
Usage
importReact,{useState}from'react';import{View,StyleSheet}from'react-native';import{Button}from'react-native-paper';import{SwipeableSnackbar}from'./SwipeableSnackbar';exportdefaultfunctionApp(){const[visible,setVisible]=useState(false);return(<Viewstyle={styles.container}><Buttonmode="contained"onPress={()=>setVisible(true)}>
Show Snackbar
</Button><SwipeableSnackbarvisible={visible}onDismiss={()=>setVisible(false)}action={{label: 'Undo',onPress: ()=>console.log('Undo pressed'),}}>
Swipe left, right, or down to dismiss!
</SwipeableSnackbar></View>);}conststyles=StyleSheet.create({container: {flex: 1,justifyContent: 'center',alignItems: 'center',},});
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
these are not working
Invariant Violation: Looks like you're passing a function component Snackbar to createAnimatedComponent function which supports only class components. Please wrap your function component with React.forwardRef() or use a class component instead.
OR
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
I want to implement swipe left or right or down to dismiss the snackbar
All reactions