From left to right window animation with react-navigation?

Viewed 6353

Is there a way to implement animation that goes from left to right with navigate() function in react-navigation ?

Thanks in advance.

4 Answers

when you define your screens, you can use transitionConfig option.

const Stack = StackNavigator(
  {
    SomeScreen: { screen: ... },
  },
  {
    transitionConfig: customAnimationFunc,
  }
);
...

import CardStackStyleInterpolator from 'react-navigation/src/views/CardStack/CardStackStyleInterpolator';

const customAnimationFunc = () => ({
  screenInterpolator: sceneProps => {
    return CardStackStyleInterpolator.forVertical(sceneProps);
  },
});

should do the job. You can of course define your own animation, or return null to disable it; check the sources for that.

I know this is old, but I also struggled to find a config that did this for only one screen at a time and ended up using react-navigation-transitions.

import {createStackNavigator} from 'react-navigation-stack';
import {fromLeft, fromRight} from 'react-navigation-transitions';
import {ScreenA, ScreenB, ScreenC} from 'screens'; 

export const handleCustomTransition = ({scenes}) => {
  const prevScene = scenes[scenes.length - 2];
  const nextScene = scenes[scenes.length - 1];

  if (
    prevScene &&
    prevScene.route.routeName === 'ScreenA' &&
    nextScene.route.routeName === 'ScreenB'
  ) {
    return fromLeft();
  } else if (
    prevScene &&
    prevScene.route.routeName === 'ScreenA' &&
    nextScene.route.routeName === 'ScreenC'
  ) {
    return fromRight();
  }
};

const stack = createStackNavigator(
  {
    ScreenA: homeScreen,
    ScreenB: messageScreen,
    ScreenC: settingsScreen,
  },
  {
    transitionConfig: nav => handleCustomTransition(nav),
  }
);

animation property on RootStack Navigator takes different values

import {createNativeStackNavigator} from '@react-navigation/native-stack'

const RootStack = createNativeStackNavigator()
return (
   <RootStack.Navigator
       screenOptions={{headerShown: false, animation: 'slide_from_right'}}>
     // Your Screens Here
   </RootStack.Navigator>
)
Related