Stack.Navigator fade-transition between Stack.Screens in React-native? - react-native

How can I add a transition effect to Stacked Screes in React-native?
<NavigationContainer>
<Stack.Navigator
screenOptions={{
headerShown: false,
}}
>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Stocks" component={StocksScreen} />
</Stack.Navigator>
</NavigationContainer>
Is there a default way to achieve a fadeIn / fadeOut effect?

The simplest way to achieve fade effect:
const forFade = ({ current }) => ({
cardStyle: {
opacity: current.progress,
},
});
If you want to apply fade effect for the entire navigator:
<Stack.Navigator
screenOptions={{
headerShown: false,
cardStyleInterpolator: forFade,
}}>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Stocks" component={StocksScreen} />
</Stack.Navigator>
Also you can apply cardStyleInterpolator for single screen via setting options:
<Stack.Screen
name="Home"
component={HomeScreen}
options={{ cardStyleInterpolator: forFade }}/>
You can customize forFade function in order to achieve other effects, or also you can use some pre-made interpolators, as:
forHorizontalIOS
forVerticalIOS
forModalPresentationIOS
forFadeFromBottomAndroid
forRevealFromBottomAndroid
import { CardStyleInterpolators } from '#react-navigation/stack';
<Stack.Screen
name="Profile"
component={Profile}
options={{
cardStyleInterpolator: CardStyleInterpolators.forFadeFromBottomAndroid,
}}
/>;
More info here: https://reactnavigation.org/docs/stack-navigator/#animations

For React Navigation 6.xx you can use the animation option:
<Stack.Screen
name="Profile"
component={Profile}
options={{ animation: 'fade' }}
/>
Supported values:
"default": use the platform default animation
"fade": fade screen in or out
"flip": flip the screen, requires presentation: "modal" (iOS only)
"simple_push": use the platform default animation, but without shadow and native header transition (iOS only)
"slide_from_bottom": slide in the new screen from bottom
"slide_from_right": slide in the new screen from right (Android only, uses default animation on iOS)
"slide_from_left": slide in the new screen from left (Android only, uses default animation on iOS)
"none": don't animate the screen

Related

React-Native Tab Navigation drawer navigation how to hide tabbar and header for every screen v6

Trying to hide header and tabbar in Tab navigation in v6
<Tab.Screen
name="LoginScreens"
component={LoginStackScreen}
options={{tabBarVisible: false}}
/>
But here tabBarVisible is not working
After researching found the solution which is different from v5
<Tab.Navigator
screenOptions={{
headerShown: false,
}}>
<Tab.Screen
name="LoginScreens"
component={LoginStackScreen}
options={{
tabBarStyle: {display: 'none'},
}}
/>
</Tab.Navigator>
tabBarStyle: {display: 'none'} to hide the tabbar and headerShown: false is to hide the header for all pages

Right-to-left screen animation in React Native navigation

I'd like to have screen animations from right to left in React Native. I checked a lot in the internet but most of the examples are old and they are using StackNavigator differently and the configurations are also different. I think they are using an old version of React Navigation. I use version 6. I also tried as explained here in docs https://reactnavigation.org/docs/stack-navigator/#animations but it didn't worked in any way.
Could you please help? Here is my code:
const Tab = createBottomTabNavigator();
const Stack = createNativeStackNavigator();
function MainScreen() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="To Read" component={ToReadScreen} />
<Tab.Screen name="Have Read" component={HaveReadScreen} />
</Tab.Navigator>
);
}
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="ReadX"
component={MainScreen}
/>
<Stack.Screen
name="Settings"
component={SettingsScreen}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
So I am trying to configure the screen animation between main screen and setting screen.
Any help would be appreaciated. Thanks.
Assuming this is a recent react-navigation v6 version, use the animationTypeForReplace option:
<Stack.Screen
name="Settings"
component={SettingsScreen}
options={{
animationTypeForReplace: 'pop',
}}
/>
https://reactnavigation.org/docs/stack-navigator/#animationtypeforreplace
<Stack.Navigator initialRouteName='LoginPage' screenOptions={{ headerShown: false, animation: 'slide_from_right' }}>
you have to use animation props in Stack.Navigator in screenOptions

How do I remove the shadow on header for React Native on Android, React Navigation v6?

The shadow looks like this.
I've tried using
headerStyle: {
elevation: 0,
shadowOpacity: 0,
borderBottomWidth: 0,
}
but according to this link
https://reactnavigation.org/docs/native-stack-navigator#options
in React Navigation v6, the only property supported in headerStyle is backgroundColor only.
For react-navigation v6 you can use headerShadowVisible to hide or show the header's shadow.
<Stack.Navigator screenOptions={{headerShadowVisible: false}}>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Notifications" component {NotificationsScreen} />
<Stack.Screen name="Profile" component={ProfileScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
</Stack.Navigator>

How can I hide the screen header but show my back button?

I would like to hide my screen header but still show the back button in my Stack Navigator? I have set screenOptions={{ headerShown: false }} in my Stack.Navigator, which hides both the screen header and back button. I would like to just hide the screen header.
Can someone please assist with this? Below is my Stack Navigator:
function SearchStack() {
return (
<Stack.Navigator screenOptions={{ headerShown: false }}>
<Stack.Screen name="SearchScreen" component={SearchScreen} />
<Stack.Screen name="SearchListScreen" component={SearchListScreen} />
</Stack.Navigator>
);
}
In the tab navigator the stack is set as:
<Tab.Navigator screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {...})}>
<Tab.Screen name="Search" component={SearchStack} />
</Tab.Navigator>
This is what I'm currently seeing:
But this is what I would like to have with my Tab navigation bar still at the bottom for the search stack:
This is what I get using options={{headerMode:"none"}} in Stack.Navigator:
The below occurs when adding updating the Stack.Navigator to <Stack.Navigator screenOptions={{ headerTitle:"", headerTransparent:true }}> . How can add or move the back button to the top exactly like the 2nd image, which is achieved when not adding the Stack to the Tab.Screen so changing:
<Tab.Screen name="Search" component={SearchStack} />
to
<Tab.Screen name="Search" component={SearchScreen} />
but doing this causes the tab to not appear in the Search list screen.
The back button is part of the header, so you can't hide the header and keep the back button.
What you want to do is to hide other parts of the header except for the back button, which would be
Title, with headerTitle: ""
Background, with headerTransparent: true
for hide the back button in react-native, we can use property,
headerBackVisible:false this property only work on android
<Stack.Screen
options={{headerBackVisible: false}}
/>
example use of in Stack
const CustomerStack = () => {
return (
<Stack.Navigator>
<Stack.Screen
name="First"
component={First}
options={{headerShown: false}}
/>
<Stack.Screen
name="Third"
component={Third}
options={{headerTitle: '', headerTransparent: true}}
/>
</Stack.Navigator>
);
}
If you don't want the default header then use like this
screenOptions={{ headerShown: false }}
and write custom code for the header with back button in your component
(If your are using class component) Then
<TouchableOpacity onPress={()=>this.props.navigation.goBack()} style={{width:'100%', height:45, flexDirection:'row'}}> <Image source={require('back button image path')}/> </TouchableOpacity>
if you want header title too then,
<TouchableOpacity onPress={()=>this.props.navigation.goBack()} style={{width:'100%', height:45, flexDirection:'row'}}> <Image source={require('back button image path')}/> <Text>SearchListScren</Text> </TouchableOpacity>
Put this code at top of the component code under a container

React navigation stack navigator disable go back ability

How to disable ability to go back
I have a functional component
export default function App(){
createHomeStackNavigator = () =>
<Stack.Navigator screenOptions={{
headerShown: false
}}>
<Stack.Screen
name="Login"
component={Login}
/>
<Stack.Screen
name="Home"
children={createHomeTabNavigator}
/>
</Stack.Navigator>
return(
<NavigationContainer>
{createHomeStackNavigator()}
</NavigationContainer>
)
When user goes to Home, its onclick on Login component then he cant go back to the login by any swipe on ios, back button on Android and other ways
Probably you should use navigation.reset();