I am working on a project that I want to switch between screens in one tab. for example I have 4 bottom tabs. and from the 1st tab, I want to move to another screen or component inside 1st tab
From this picture, I want to move to another screen or component by staying inside 1st tab.
Could anyone help me to get the possible way.
You need to nest navigators which is a basic feature of react-native-navigation.
This is described in the documentation here.
Navigating to a screen works as usual as it is described in the documentation here.
Here is a minimal working example for which I have made a working snack.
App.js
import React from 'react';
import {NavigationContainer} from '#react-navigation/native';
import Tabs from './Tabs'
export default function App() {
return (
<NavigationContainer>
<Tabs />
</NavigationContainer>
)
}
Tabs.js with 2 Tabs
import React from 'react';
import {createBottomTabNavigator} from '#react-navigation/bottom-tabs';
import Home from './Home.js'
import Profile from './Profile.js'
const Tab = createBottomTabNavigator();
function Tabs() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Profile" component={Profile} />
</Tab.Navigator>
);
}
export default Tabs;
Home.js a StackNavigator
import React from 'react';
import {createNativeStackNavigator} from '#react-navigation/native-stack';
import ScreenA from './ScreenA.js'
import ScreenB from './ScreenB.js'
const Stack = createNativeStackNavigator()
function Home() {
return <Stack.Navigator>
<Stack.Screen name="" component={ScreenA} options={{headerShown: false}} />
<Stack.Screen name="ScreenB" component={ScreenB} />
</Stack.Navigator>
}
export default Home;
ScreenA.js
import React from 'react'
import { View, Button } from 'react-native'
function ScreenA(props) {
return <View>
<Button title="NavigateToScreenBInTabHone" onPress={() => props.navigation.navigate('ScreenB')}>
</Button>
</View>
}
export default ScreenA;
ScreenB.js
import React from 'react'
import { View } from 'react-native'
function ScreenB() {
return <View></View>
}
export default ScreenB;
Profile.js
import React from 'react'
import { View } from 'react-native'
function Profile() {
return <View></View>
}
export default Profile;
Related
I'm started with React Native and try to use the navigation between screns. To do so I creat a component Navigation but when I want to use it this error show up :
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined
It appear to be the component AppNavigator that is the issue
I tried :
checked all my exports and import and for me their is no problems
change my const component in a function component but nothing do the trick.
Can you help me ?
App.js :
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import AppNavigator from './components/navigation';
function App() {
return (
<View>
<AppNavigator />
</View>
);
}
/*const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
justifyContent: 'center',
},
});*/
export default App;
Navigation.js
import React from 'react';
import { createStackNavigator } from '#react-navigation/stack';
import { NavigatorContainer } from '#react-navigation/native';
import Localisation from './localisation';
import Home from './home';
const { Navigator, Screen } = createStackNavigator()
const AppNavigator = () => (
<NavigatorContainer>
<Navigator initialRouteName="Home">
<Screen options={{headerShown: false}} name="Home" component={Home} />
</Navigator>
</NavigatorContainer>
)
export default AppNavigator;
There is no component named NavigatorContainer in react-navigation/native. The component is called NavigationContainer.
Hence, the following should fix the issue.
import React from 'react';
import { createStackNavigator } from '#react-navigation/stack';
import { NavigationContainer } from '#react-navigation/native';
import Localisation from './localisation';
import Home from './home';
const { Navigator, Screen } = createStackNavigator()
const AppNavigator = () => (
<NavigationContainer>
<Navigator initialRouteName="Home">
<Screen options={{headerShown: false}} name="Home" component={Home} />
</Navigator>
</NavigationContainer>
)
export default AppNavigator;
I'm starting to learn react native and I'm trying to change the header color but when I run the app only the text color changed to white. And the terminal shows some warning. Are both of them related? Here are some screenshots of it.
import 'react-native-gesture-handler';
import { setStatusBarBackgroundColor, StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet} from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import LoginScreen from './screens/LoginScreen';
const globalScreenOptions ={
headerStyle: { BackgroundColor:"dodgerblue" },
headerTitleStyle: {color: "white"},
headerTinColor:"white",
};
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator screenOptions={globalScreenOptions} >
<Stack.Screen name='Login' component={LoginScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
enter image description here
enter image description here
Try This its working you must provide style on Stack.Screen
<Stack.Screen options={{headerStyle:{backgroundColor:'dodgerblue'}}}name="Settings" component={SettingsScreen} />
I am using react native and react navigation. I am trying to create a nested navigator, i.e., I am trying to place a tab navigator within a stack navigator. When I run the app on iOS simulator, however, the main screen reloads infinitely. What might be wrong with the below code? New to react native...bear with me if this is a simple one!
Below is the part of the code where I am doing the nesting:
import React from 'react';
import Home from './routes/Home'
import Alert from './routes/Alert'
import Profile from './routes/Profile'
import Subs from './routes/Subs'
import Write from './routes/Write'
import OtherProfile from './routes/OtherProfile'
import Post from './routes/Post'
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import { SafeAreaView } from 'react-native-safe-area-context';
export default function App() {
const MainStack = createStackNavigator();
const Tab = createBottomTabNavigator();
function HomeTab() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={Home}/>
<Tab.Screen name="Subs" component={Subs}/>
<Tab.Screen name="Write" component={Write} options={{ tabBarVisible: false }}/>
<Tab.Screen name="Alert" component={Alert}/>
<Tab.Screen name="Profile" component={Profile}/>
</Tab.Navigator>
)
}
function MainStackScreen() {
return (
<MainStack.Navigator>
<MainStack.Screen name="HomeTab" component={HomeTab} options = {{ headerShown: false }} />
<MainStack.Screen name="Post" component={Post} />
<MainStack.Screen name="OtherProfile" component={OtherProfile} />
</MainStack.Navigator>
)
}
return (
<NavigationContainer>
<SafeAreaView style={{flex:1}}>
<MainStackScreen/>
</SafeAreaView>
</NavigationContainer>
)
}
Found the answer right after posting this.
I declared the navigators and the navigation components outside of the App component and it works.
I'm new to React Native, maybe it's a weird question,
I have a simple app with a BottomTabNavigator with 4 tabs in it: A, B, C, D.
on screen "A" I have a button that when you click on you will go to screen "E" that is not suppose to be visible on the bottom tabs.
I tried to add that navigation.navigate("E") but it seems to work only when I actually add that E to the bottomTabs. But I don't need that E screen visible on the bottom tabs, and E should be found only by going to A and pressing on that button.
Any idea how can I do that?
STACK
(App.js)
import React, { Component } from 'react'
import { createStackNavigator } from "#react-navigation/stack";
import { NavigationContainer } from "#react-navigation/native";
const Stack = createStackNavigator()
export default class App extends Component {
render() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName={"Main"}>
<Stack.Screen name="Main" component={MainScreen} style={{
headerShown: false}} />
<Stack.Screen name="screenE" component={screenE} />
</Stack.Navigator>
<NavigationContainer/>
)
}}
TAB
(Main Screen)
import React, { Component } from 'react'
import { createBottomTabNavigator } from "#react-navigation/bottom-tabs";
import { NavigationContainer } from "#react-navigation/native";
const Tab = createBottomTabNavigator()
export default class Main extends Component {
render() {
return (
<Tab.Navigator initialRouteName="screenA">
<Tab.Screen name="screenA" component={screenA} />
<Tab.Screen name="screenB" component={screenB} />
<Tab.Screen name="screenC" component={screenC} />
<Tab.Screen name="screenD" component={screenD} />
</Tab.Navigator>
)}}
If you have any questions please don't hesitate to ask. If there is anything wrong it please feel free to comment.
I want to create a splashscreen without the whitescreen issue using the react-native splash screen and after that I want to check the navigation. Suppose I got 3 screen Splash, Login and Home. If the user is logged in I want to navigate to the Home screen from the Splash screen otherwise I want to navigate it to the Login page from the Splash screen. How do I achieve this using react-native-splash-screen and react-navigation.
App.js
import React from 'react';
import {ActivityIndicator} from 'react-native';
import {Provider} from 'react-redux';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {PersistGate} from 'redux-persist/es/integration/react';
import Navigator from './app/navigation/NavigationStack';
import configureStore from './app/store/configureStore';
const {persistor, store} = configureStore();
export default function App() {
return (
<SafeAreaProvider>
<Provider store={store}>
<PersistGate loading={<ActivityIndicator />} persistor={persistor}>
<Navigator></Navigator>
</PersistGate>
</Provider>
</SafeAreaProvider>
);
}
NavigationStack.js
function MyStack() {
return (
<NavigationContainer ref={navigationRef}>
<Stack.Navigator
screenOptions={{
headerShown: false,
}}>
<Stack.Screen
name={Strings.string_SignInScreen}
component={SignInScreen}
/>
<Stack.Screen name={Strings.string_HomeScreen} component={Home} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default MyStack;
You can do something like this,in its componentDidMount cehck fro user exits via asyncstorage etc, and then navigate accordingly
import SplashScreen from 'react-native-splash-screen'
export default class WelcomePage extends Component {
componentDidMount() {
// do stuff while splash screen is shown
// After having done stuff (such as async tasks) hide the splash screen
if(userExists){
SplashScreen.hide();
this.props.navigation.navigate('Homescreen');
} else {
this.props.navigation.navigate('LoginScreen');
}
}
}
Hope it helps. feel free for doubts