Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to navigate to tab while using a custom tab when a new tab/screen is dynamically added at random intervals #11971

Open
3 of 11 tasks
akashgupta17 opened this issue May 1, 2024 · 5 comments

Comments

@akashgupta17
Copy link

akashgupta17 commented May 1, 2024

Current behavior

Here's my code :

import React, { useEffect, useState } from 'react';
import { Animated, SafeAreaView, StyleSheet, TouchableOpacity, View } from 'react-native';

import { NavigationContainer } from '@react-navigation/native';
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';

const Tab = createMaterialTopTabNavigator();

function MyTabBar({ state, descriptors, navigation, position }) {
	return (
		<View style={{ flexDirection: 'row' }}>
			{state.routes.map((route, index) => {
				const { options } = descriptors[route.key];
				const label =
					options.tabBarLabel !== undefined
						? options.tabBarLabel
						: options.title !== undefined
						? options.title
						: route.name;

				const isFocused = state.index === index;

				const onPress = () => {
					const event = navigation.emit({
						type: 'tabPress',
						target: route.key,
						canPreventDefault: true,
					});

					if (!isFocused && !event.defaultPrevented) {
						navigation.navigate(route.name, route.params);
					}
				};

				const onLongPress = () => {
					navigation.emit({
						type: 'tabLongPress',
						target: route.key,
					});
				};

				const inputRange = state.routes.map((_, i) => i);
				const opacity = position.interpolate({
					inputRange,
					outputRange: inputRange.map((i) => (i === index ? 1 : 0)),
				});

				return (
					<TouchableOpacity
						accessibilityRole="button"
						accessibilityState={isFocused ? { selected: true } : {}}
						accessibilityLabel={options.tabBarAccessibilityLabel}
						testID={options.tabBarTestID}
						onPress={onPress}
						onLongPress={onLongPress}
						style={{
							flex: 1,
							height: 50,
							backgroundColor: 'green',
							alignItems: 'center',
							justifyContent: 'center',
						}}
					>
						<Animated.Text>{label}</Animated.Text>
					</TouchableOpacity>
				);
			})}
		</View>
	);
}

const HomeScreen = () => {
	return <View style={{ backgroundColor: 'yellow', flex: 1 }}></View>;
};

const SettingsScreen = () => {
	return <View style={{ backgroundColor: 'red', flex: 1 }}></View>;
};

const NewScreen = () => {
	return <View style={{ backgroundColor: 'pink', flex: 1 }}></View>;
};

function MyTabs() {
	const [state, setstate] = useState([
		{ name: 'Home', component: HomeScreen },
		{ name: 'Settings', component: SettingsScreen },
	]);
	useEffect(() => {
		setTimeout(() => {
			const newState = state.slice();
			newState.splice(1, 0, { name: 'New Screen', component: NewScreen });
			setstate(newState);
		}, 15000);
	}, []);
	return (
		<Tab.Navigator tabBar={(props) => <MyTabBar {...props} />}>
			{state.map((item) => {
				return <Tab.Screen key={item.name} name={item.name} component={item.component} />;
			})}
		</Tab.Navigator>
	);
}

const App = () => {
	return (
		<SafeAreaView style={{ flex: 1 }}>
			<NavigationContainer>
				<MyTabs />
			</NavigationContainer>
		</SafeAreaView>
	);
};

const styles = StyleSheet.create({
	sectionContainer: {
		marginTop: 32,
		paddingHorizontal: 24,
	},
	sectionTitle: {
		fontSize: 24,
		fontWeight: '600',
	},
	sectionDescription: {
		marginTop: 8,
		fontSize: 18,
		fontWeight: '400',
	},
	highlight: {
		fontWeight: '700',
	},
});

export default App;

Screen.Recording.2024-05-01.at.11.56.11.PM.mov

Expected behavior

It should navigate to the tab which is being selected in the video

Reproduction

Create a tab while by a state and add another tab to the state after some time -> while this happens stay on the last tab -> after the new tab is added before the index of the current focused tab -> try to navigate to the newly added tab by not SWIPING but clicking on the tab name in custom tab bar

Platform

  • Android
  • iOS
  • Web
  • Windows
  • MacOS

Packages

  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-top-tabs
  • @react-navigation/stack
  • @react-navigation/native-stack
  • react-native-tab-view

Environment

  • [] I've removed the packages that I don't use
package version
@react-navigation/native 6.1.17
@react-navigation/bottom-tabs
@react-navigation/drawer
@react-navigation/material-top-tabs 6.6.13
@react-navigation/stack
@react-navigation/native-stack
react-native-safe-area-context
react-native-screens
react-native-gesture-handler
react-native-reanimated
react-native-tab-view 3.5.2
react-native-pager-view 6.3.1
react-native 0.72.5
expo
node
npm or yarn
Copy link

github-actions bot commented May 1, 2024

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/native
  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-top-tabs
  • @react-navigation/stack
  • react-native-tab-view

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

Copy link

github-actions bot commented May 1, 2024

Hey @akashgupta17! Thanks for opening the issue. It seems that the issue doesn't contain a link to a repro.

The best way to get attention to your issue is to provide an easy way for a developer to reproduce the issue.

You can provide a repro using any of the following:

A snack link is preferred since it's the easiest way to both create and share a repro. If it's not possible to create a repro using a snack, link to a GitHub repo under your username is a good alternative. Don't link to a branch or specific file etc. as it won't be detected.

Try to keep the repro as small as possible by narrowing down the minimal amount of code needed to reproduce the issue. Don't link to your entire project or a project containing code unrelated to the issue. See "How to create a Minimal, Reproducible Example" for more information.

You can edit your original issue to include a link to the repro, or leave it as a comment. The issue will be closed automatically after a while if you don't provide a repro.

Copy link

github-actions bot commented May 1, 2024

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/native
  • @react-navigation/material-top-tabs
  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/stack
  • react-native-tab-view

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

Copy link

github-actions bot commented May 1, 2024

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/stack
  • react-native-tab-view

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

Copy link

github-actions bot commented May 1, 2024

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/stack

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

@akashgupta17 akashgupta17 changed the title Not able to navigate to tab while using a custom tab when a new tab/screen is dynamically added Not able to navigate to tab while using a custom tab when a new tab/screen is dynamically added at random intervals May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant