Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable animation for a custom header in React Navigation

I would like to disable the screen animation for the header part of the Stack Navigator.

I have a common custom Header defined in the Stack Navigator via screenOptions.

And have default animations for screen transitions. I want to make sure the animation happens only to the screen and not to my header component. Since the header will a static content.

I've also tried making the headerMode as screen and float but that did not help. I wanted to see if there is a property similar to animationEnabled but for the header component.

<Stack.Navigator
  screenOptions= {{
    headerMode: 'screen',
    animation: 'fade',
    header: (props) =>
        <Header {...props} />
  }}>
  // Rest of my screens
</Stack.Navigator>
like image 432
AbsoluteSith Avatar asked May 26 '26 01:05

AbsoluteSith


1 Answers

What you could do is completely separate the header from your Navigator, and use a ref to control navigation from it. Something like this:

const App = () => {
  const navigationRef = useNavigationContainerRef()

  return (
    <View>
      <Text>This header won't animate!</Text>
      <Text onPress={() => navigationRef.navigate('Home')}>Link</Text>
    </View>
    <NavigationContainer ref={navigationRef}>
      <Stack.Navigator screenOptions={{ headerShown: false }}>
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Other" component={OtherScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  )
}
like image 174
rkok Avatar answered May 31 '26 13:05

rkok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!