Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

White background flashing when switching screens - React-Navigation v5

I'm migrating a RN project version 4 to 5.

When switching screens there was an issue with a white background flashing in. In v4 this was solved by setting cardStyle: { backgroundColor: material.containerBgColor } in the StackNavigation options.

However in v5 I'm unable to fix it with the same approach:

<Stack.Navigator cardStyle={{ backgroundColor: material.containerBgColor }} ...>

White flash has come back. Any idea how to fix it? Thanks.

Update: The structure of the navigation may be important:

const AppTabNavigator = () => (   <Tab.Navigator>     <Tab.Screen name="Home" component={Home} />     <Stack.Screen name="ScreenD" component={ScreenD} />     <Stack.Screen name="ScreenE" component={ScreenE} />     <Stack.Screen name="ScreenF" component={ScreenF} />   </Tab.Navigator> ) ...   <Stack.Navigator     ...     cardStyle={{ backgroundColor: material.containerBgColor }}   >     <Stack.Screen name="Home" component={AppTabNavigator} />     <Stack.Screen name="ScreenA" component={ScreenA} />     <Stack.Screen name="ScreenB" component={ScreenB} />     <Stack.Screen name="ScreenC" component={ScreenC} />   </Stack.Navigator> 

Going from ScreenD to ScreenE does the flashing issue. I'm not sure about the other screens as they don't make any network request / async stuff.

like image 575
haxpanel Avatar asked Jan 24 '20 17:01

haxpanel


People also ask

How do you turn off back swipe gesture react navigation?

To make this work, you need to: Disable the swipe gesture for the screen ( gestureEnabled: false ). Override the native back button in the header with a custom back button ( headerLeft: (props) => <CustomBackButton {... props} /> ).

Which is better react navigation or React Native navigation?

Integration with existing apps In such a case, any navigator based on JS works well compared to existing native solutions. This gives React Navigation the edge over RNN, especially if you are looking to power a few modules of your existing app with React Native, just like Facebook and many other brownfield apps do.

Does react navigation use reanimated?

React Navigation related packages include @react-navigation/drawer , @react-navigation/stack , @react-navigation/bottom-tabs and @react-navigation/material-top-tabs use react-native-gesture-handler and react-native-reanimated for smooth gestures and animations.

Does react navigation work with react?

React Navigation's web support currently requires using React Native for Web. This approach lets us reuse the same code on both React Native and Web.


2 Answers

I faced the same issue and dived into an investigation. It seems that the detachment of the screens causes it. I found a few approaches. You can choose one according to your needs. They are the following:

  1. You can specify a view wrapper of the navigator with the same background color as the screens one like:

    <View style={{ flex: 1, backgroundColor: '#YOUR_SCREEN_COLOR' }}>   // It could be your NavigationContainer or your StackNavigator depends on your goals    <Navigator />  </View> 
  2. You can also specify your screen mode to be modal in the stack view config this prevents the screens from being detached like:

    <StackNavigator.Navigator mode="modal">   {/*.... Your stack screens ... */} </StackNavigator.Navigator> 
  3. You can add your custom overlay in screenOptions by using the cardOverlay prop:

    cardOverlay: () => (   <View     style={{     flex: 1,     backgroundColor: '#YOUR_COLOR',   }} />) 

    Reference: https://reactnavigation.org/docs/stack-navigator/#cardoverlay

  4. You can use the cardStyleInterpolator:

    This allows you to customize the animation transitions when navigating from screen to screen.

    Here are the snippets from the original documentation:

    const forFade = ({ current, closing }) => ({   cardStyle: {     opacity: current.progress,   }, }); 
    <Stack.Screen   name="Profile"   component={Profile}   options={{ cardStyleInterpolator: forFade }} /> 

    Stack Navigator exposes various options to configure the transition animation when a screen is added or removed.

    Reference: https://reactnavigation.org/docs/stack-navigator/#animation-related-options

like image 161
nikolay.hristov Avatar answered Sep 29 '22 01:09

nikolay.hristov


Fixed it by using the DarkTheme for the Navigation Container

import { NavigationContainer, DarkTheme } from '@react-navigation/native';  return (     <NavigationContainer theme={DarkTheme}>        {children}     </NavigationContainer>  
like image 34
Michael Pomogajko Avatar answered Sep 29 '22 03:09

Michael Pomogajko