Navigating to a new view - react-native

How to correctly navigate to a new view?
App:
TabNavigator (Top of the screen)
StackNavigator (Bottom of the screen)
I want that after choosing a button from StackNavigator opened a new screen that will override the entire application. I do not want to see TabNavigator and StackNavigator.
In the new window, I want it to be NavBar with the return button
All the tutorials that I'm watching show how to switch between screens but I can not find the situation above.
I want to open a new window from the main application and then return to it
EDITED:
const TopNavTabs = TabNavigator({
General: { screen: General },
Help: { screen: Help },
Tips: { screen: Tips },
}
});
export const Navigation = StackNavigator(
{
Tab: { screen: TopNavTabs },
Article: { screen: Article },
}
);
export default class App extends Component{
render(){
return(
<View>
<View>
<Navigation navigation={this.props.navigation} />
</View>
<View>
<View>
<MCIcon name="account"/>
</View>
<View>
<MIcon name="create" onPress={() => this.props.navigation.navigate('Article')} />
</View>
<View>
<FAIcon name="hashtag" />
</View>
<View>
<FAIcon name="search" />
</View>
</View>
</View>
)
}
}

Add TabNavigator in StackNavigator like:
const TopNavTabs = TabNavigator(
{
General: { screen: General },
Help: { screen: Help },
Tips: { screen: Tips },
},
{
navigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) => {
const { routeName } = navigation.state;
let iconName;
if (routeName === 'Home') {
iconName = `ios-information-circle${focused ? '' : '-outline'}`;
} else if (routeName === 'Settings') {
iconName = `ios-options${focused ? '' : '-outline'}`;
}
// You can return any component that you like here! We usually use an
// icon component from react-native-vector-icons
return <Ionicons name={iconName} size={25} color={tintColor} />;
},
}),
tabBarOptions: {
activeTintColor: 'tomato',
inactiveTintColor: 'gray',
},
tabBarComponent: TabBarBottom,
tabBarPosition: 'bottom',
animationEnabled: false,
swipeEnabled: false,
}
);
export const Navigation = StackNavigator(
{
Tab: { screen: TopNavTabs },
Article: { screen: Article },
}
);
export default class App extends Component{
render(){
return(
<Navigation />
)
}
}

Related

How to hide bottom tab navigation in login screen?

I want to hide the bottom tab bar in login page and show it to the other screens how can i achieve this?
right now there's bottom tab bar at the login screen i want to remove it and show it once i signed in. if anyone knows how to this please help. i got some idea from this answer React Native: How to hide bottom bar before login and show it once the user is logged in? but I have no idea how to check weather the user is signed in or not ! how can i pass it from my login screen to app.js?
this is my app.js
import { createStackNavigator } from "react-navigation-stack";
import { createAppContainer, createSwitchNavigator} from "react-navigation";
import LoginScreen from './src/screens/login';
import Orders from './src/screens/home';
import TransactionScreen from './src/screens/transactionScreen';
import Logout from './src/screens/footer';
import { Icon } from "react-native-elements";
import * as React from "react";
import { View, Image, Text, ActivityIndicator ,SafeAreaView,StatusBar,Alert} from "react-native";
import { createBottomTabNavigator } from 'react-navigation-tabs';
import AsyncStorage from "#react-native-community/async-storage";
// const Apps = createStackNavigator({
// LoginScreen: { screen: LoginScreen },
// Home :{
// screen:Home
// },
// TransactionScreen:{
// screen:TransactionScreen,
// navigationOptions: {
// headerTitle: "Transactions",
// headerStyle: {
// backgroundColor: "white",
// },
// headerTintColor: "black",
// },
// }
// });
// const App = createAppContainer(Apps);
// export default App;
class IconWithBadge extends React.Component {
render() {
const { name, badgeCount, color, size,type } = this.props;
return (
<View style={{ width: 24, height: 24 }}>
<Icon name={name} size={size} type={type} color={color} />
</View>
);
}
}
const getTabBarIcon = (navigation, focused, tintColor) => {
const { routeName } = navigation.state;
let IconComponent = Icon;
let iconName;
let type =null;
if (routeName === "Orders") {
iconName = `kitchen`;
} else if (routeName === "Transactions") {
iconName = `account-balance`;
} else if (routeName === "Logout") {
iconName = `settings`;
type="font-awesome"
}
// You can return any component that you like here!
return <IconWithBadge name={iconName} type={type} color={tintColor} />;
};
const AuthStack = createStackNavigator({
LoginScreen: { screen: LoginScreen },
});
const RootStack = createBottomTabNavigator(
{
Orders: { screen: Orders },
Transactions: { screen: TransactionScreen },
Logout: {
screen: Logout,
navigationOptions:({navigation}) => ({
tabBarOnPress:(scene, jumpToIndex) => {
return Alert.alert(
"Confirmation Required",
'Do you want to logout?',
[
{text:"Yes", onPress: ()=>{AsyncStorage.clear(); navigation.navigate('Auth')}},
{text:"Cancel"}
]
)
}
})
}
},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) =>
getTabBarIcon(navigation, focused, tintColor),
}),
tabBarOptions: {
// initialRouteName: "FirstScreen",
activeTintColor: "yellow",
activeBackgroundColor: "#023333",
inactiveTintColor: "white",
inactiveBackgroundColor: "#023333",
upperCaseLabel: true,
showIcon: true,
barStyle: { backgroundColor: "#fff" },
lazy: false,
},
}
);
class AuthLoadingScreen extends React.Component{
constructor(props){
super(props);
this._loadData();
}
_loadData = async() =>{
const isLoggedIn = await AsyncStorage.getItem('isLoggedIn');
this.props.navigation.navigate(isLoggedIn !== '1'? 'Auth' : 'App');
}
render(){
return(
<SafeAreaView style={{flex:1,justifyContent:'center',alignItems:'center'}}>
<StatusBar barStyle="dark-content"/>
<ActivityIndicator/>
</SafeAreaView>
);
}
}
export default createAppContainer(createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
App:RootStack,
Auth:AuthStack
},
{
initialRouteName:'AuthLoading'
}
))
Looks like you're using navigation-stack and navigation-tabs.
For that I have a solution. then make a structure like this.
import {createBottomTabNavigator} from '#react-navigation/bottom-tabs';
import {createStackNavigator} from '#react-navigation/stack';
if you want to display bottom navigation. create a tab screen function like this.
function HomeTabs() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Feed" component={Feed} />
<Tab.Screen name="Notifications" component={Notifications} />
</Tab.Navigator>
);
}
then call in navigation container.
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={HomeTabs}
/>
<Stack.Screen
name="Login"
component={LoginScreen}
/>
<Stack.Screen
name="Signup"
component={SignupScreen}
/>
)}
</Stack.Navigator>
</NavigationContainer>
After re-organizing the navigation structure, now if we navigate to the Login or Signup screens, the tab bar won't be visible over the screen anymore.
ref : https://reactnavigation.org/docs/hiding-tabbar-in-screens/

React Navigation drawer doesn't display the title of the current page

I created 2 pages namely Home and Profile and added these two pages to createDrawerNavigation. And then added this drawer navigation to a stack navigator. Now when I try to display the title on the header of the current page. I'm unable to do it.
Here is my code.
Tried DefaultNavigationOptions and got the props value of navigator but doesn't work that way.
const MenuStack = createDrawerNavigator(
{
HomePage: {
screen: Home
},
ProfilePage: {
screen: Profile
}
},
{
drawerWidth: Math.round(Dimensions.get("window").width) * 0.75,
contentOptions: {
activeTintColor: theme.colors.accent
}
}
);
const AppStack = createStackNavigator(
{
MenuStack: {
screen: MenuStack
}
},
{
defaultNavigationOptions: ({ navigation }) => {
return {
headerLeft: (
<Icon
style={{ paddingLeft: 10 }}
onPress={() => navigation.toggleDrawer()}
name="bars"
size={30}
/>
),
headerRight: (
<Icon
style={{ paddingRight: 10 }}
onPress={() => navigation.toggleDrawer()}
name="search"
size={30}
/>
),
headerTitle: navigation.state.routeName,
headerStyle: {
backgroundColor: theme.colors.accent
}
};
}
}
);
export const createRootNavigator = (signedIn = false) => {
return createAppContainer(
createSwitchNavigator(
{
SignedIn: {
screen: AppStack
},
SignedOut: {
screen: WelcomeStack
},
SignInAndOut: {
screen: AuthStack
}
},
{
initialRouteName: signedIn ? "SignedIn" : "SignedOut"
}
)
);
};
I added a headerTitle property in the createStackNavigator but I tell the "menustack" as the title for every page.
Expected the title of the page on the header.
You have 2 options:
1. Render your own header component in each screen
You can render a header inside your screens, either a custom one or one from a component library such as react native paper.
function Home() {
return (
<React.Fragment>
<MyCustomHeader title="Home" />
{{ /* screen content */ }}
</React.Fragment>
);
}
2. Create a stack navigator for each screen that needs to have header
const MenuStack = createDrawerNavigator({
HomePage: {
screen: createStackNavigator({ Home })
},
ProfilePage: {
screen: createStackNavigator({ Profile })
}
});
Keep in mind that while this requires less code, it's also less performant.

Hide bottom tab naivgation

I have a bottom tab bar that locates in app.js. And I have the class where I want to hide the bottom bar. In page home.js I have 2 classes. 1st one is main (is the list of articles), the second one is for button page navigation (in this class I display articles). How I can hide bottom tab navigation in the second page (where articles are displayed). I have tried tabBarVisible: false, but this does not work. Help me, please.
Code:
// app.js
const TabNavigator = createBottomTabNavigator({
Home:{
screen:Home,
navigationOptions:{
tabBarLabel:'Главная',
tabBarIcon:({tintColor})=>(
<Icon name="ios-home" color={tintColor} size={24} />
)
}
},
Courses:{
screen:Courses,
navigationOptions:{
tabBarLabel:'Courses',
tabBarIcon:({tintColor})=>(
<Icon name="ios-school" color={tintColor} size={24} />
)
}
},
Editor:{
screen:Editor,
navigationOptions:{
tabBarLabel:'Editor',
tabBarIcon:({tintColor})=>(
<Icon name="ios-document" color={tintColor} size={24} />
)
}
},
},{
tabBarOptions:{
activeTintColor:'#db0202',
inactiveTintColor:'grey',
style:{
fontSize:3,
height:45,
backgroundColor:'white',
borderTopWidth:0,
elevation: 5
}
}
});
export default createAppContainer(TabNavigator);
// home.js
import React from 'react';
import { Font } from 'expo';
import { Button, View, Text, SafeAreaView, ActivityIndicator, ListView, StyleSheet, Image, Dimensions,
ScrollView } from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation'; // Version can be specified in package.json
import Icon from 'react-native-vector-icons/Ionicons'
import Courses from './Courses'
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Home',
};
const { navigate } = this.props.navigation;
return (
<SafeAreaView style={styles.MainContainer}>
<ScrollView
>
<ListView
dataSource={this.state.dataSource}
renderSeparator={this.ListViewItemSeparator}
renderRow={rowData => (
<>
<Text
onPress={() => {
/* 1. Navigate to the Details route with params */
this.props.navigation.navigate("Articles", {
otherParam: rowData.article_title,
});
}}
>
{rowData.article_title}
</Text>
</>
)}
/>
</ScrollView
>
</SafeAreaView>
);
}
}
class ArticleScreen extends React.Component {
static navigationOptions = ({ navigation, navigationOptions }) => {
const { params } = navigation.state;
return {
title: params ? params.otherParam : '',
};
};
render() {
const { params } = this.props.navigation.state;
const article_title = params ? params.otherParam : '';
return (
<Text>{article_title}</Text>
);
}
}
const RootStack = createStackNavigator(
{
Home: {
screen: HomeScreen,
},
Courses: {
screen: Courses,
navigationOptions: {
header: null,
}
},
Articles: {
screen: ArticleScreen,
},
},
{
initialRouteName: 'Home',
}
);
const AppContainer = createAppContainer(RootStack);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
const AppContainer = createAppContainer(RootStack);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
You have to make StackNavigator as main Navigator and TabBar as a sub navigator:
const TabBar = createBottomTabNavigator(RouteConfigs, TabNavigatorConfig);
const MainNavigator = createStackNavigator(
{
TabBar,
WelcomeScene: { screen:Scenes.WelcomeScene },
HomeScene: { screen: HomeScene }
}
Using this when you go the second screen Tabbar will hide automatically.
Can you try this?
class ArticleScreen extends React.Component {
static navigationOptions = ({ navigation }) => {
return {
title: params ? params.otherParam : '',
tabBarVisible: false
};
};
...
How about something like this. Create Tab navigator and pass it down to Stack navigator as one of the screen, when you navigate to the Articles, it will hide the tab bar...
const TabNavigator = createBottomTabNavigator({
Home: {
screen: Home,
navigationOptions: {
tabBarLabel: 'Главная',
tabBarIcon: ({ tintColor }) => (
<Icon name="ios-home" color={tintColor} size={24} />
),
},
},
Courses: {
screen: Courses,
navigationOptions: {
tabBarLabel: 'Courses',
tabBarIcon: ({ tintColor }) => (
<Icon name="ios-school" color={tintColor} size={24} />
),
},
},
Editor: {
screen: Editor,
navigationOptions: {
tabBarLabel: 'Editor',
tabBarIcon: ({ tintColor }) => (
<Icon name="ios-document" color={tintColor} size={24} />
),
},
},
}, {
tabBarOptions: {
activeTintColor: '#db0202',
inactiveTintColor: 'grey',
style: {
fontSize: 3,
height: 45,
backgroundColor: 'white',
borderTopWidth: 0,
elevation: 5,
},
},
});
const stackNavigator = createStackNavigator({
Home: {
screen: TabNavigator,
navigationOptions: {
header: null,
},
},
Articles: {
screen: ArticleScreen,
},
// add screens here which you want to hide the tab bar
});
export default createAppContainer(stackNavigator);

'DrawerOpen' working but invisible contents in react-native

Using react-native
"react": "16.0.0",
"react-native": "0.50.4",
and Using 'react-navigation 1.0'
everything works well but when i press drawer menu to open it's working to open but its invisible only shows underlay color (black)
i tried to remove header component on the other stack navigator..or look for problem inside DrawerComponent. but still can not figure out.
it worked fine when i didn't change to Tab Navigation though, i needed to add Tab Navigator so changed it.
please help me
const RootNavigator = StackNavigator({
Init : { screen : InitComponent },
Root : { screen : TabStack },
})
const TabStack = TabNavigator({
DrawerStack: { screen : DrawerStack, },
AddrDrawerStack : {screen : AddrDrawerStack},
StorageDrawerStack : {screen : StorageDrawerStack},
},
{
navigationOptions:({navigation})=>({
header:<HeaderContainer navigation={navigation} />,
tabBarIcon : ({focused, tintColor}) =>{
const { routeName } = navigation.state;
return <FontAwesome size={25} color={tintColor}>{icon}</FontAwesome>;
},
}),
tabBarComponent: TabBarBottom,
tabBarPosition: 'bottom',
tabBarOptions: {
activeTintColor: 'tomato',
inactiveTintColor: 'gray',
},
//animationEnabled: false,
swipeEnabled: true,
})
const DrawerStack = DrawerNavigator({
MailStack : {screen: MailStack},
}, {
gesturesEnabled: false,
contentComponent: DrawerContainer,
})
** DrawerContainer.js **
class DrawerContainer extends React.Component {
constructor(props, context){
super(props,context);
this.state = {
selectMenu: false,
}
this.props.getMailboxMenuList();
this.menuClick = this.menuClick.bind(this);
}
menuClick(menuName){
this.setState({
selectMenu: true,
});
this.props.mailBoxChange(menuName)
this.props.navigation.navigate("DrawerStack", {menu:menuName});
}
render() {
const { navigation, mailboxMenuList } = this.props;
if(!mailboxMenuList){
return (
<View style={styles.container}>
<Text>try again</Text>
</View>
)
}
return (
<View style={styles.container}>
<View style={styles.menuMailTop}>
<TouchableHighlight
onPress={()=>{this.props.navigation.navigate("PostScreen",{type:'write'})}}
style={styles.menuMailBoxBtn} >
<Text> write </Text>
</TouchableHighlight>
</View>
<View>
{
mailboxMenuList.items? <BasicMailBox
mailList={mailboxMenuList.items}
onReloadClick={this.props.reloadExternalMailbox.bind(this)}
externalLoading={this.props.externalLoading}
selectMenu ={this.state.selectMenu}
menuClick={this.menuClick.bind(this)} />
:<View></View>
}
</View>
</View>
) }
}
** Drawer open method**
<TouchableHighlight
onPress= {() => {
//when i press this, index indicates 0
if (navigation.state.index === 0) {
navigation.navigate('DrawerOpen')
// navigation.openDrawer()
} else {
navigation.navigate('DrawerClose')
}
}
} >
</TouchableHighlight>

Open Drawer by clicking in header of containing StackNavigator

This is the component which contains my Drawer
export default class StackInView extends React.Component {
render() {
const Stack = StackNavigator({
DrawerStack: { screen: DrawerInView }
}, {
headerMode: 'float',
});
return (
<View style={{ flex: 1 }}>
<Stack />
</View>
);
}
}
The following is where I define my button. I want to define the button in navigationOptions of the screen, because the button should only appear on the screen with the drawer. But clicking the button doesn't work can you help me pls?
... imports ...
export default class DrawerInView extends React.Component {
static navigationOptions = {
title: "Yeah?",
headerRight: <Button title="Menu" onPress={() => {NavigationActions.navigate("DrawerOpen")}}/>
}
constructor(props) {
super(props);
}
render() {
const Drawer = DrawerNavigator({
"one": {
screen: () => {
return (<TabsInView select="something" />)
},
},
"two": {
screen: () => {
return (<TabsInView select="something else" />)
},
}
})
return (
<View style={{ flex: 1 }}>
<Drawer />
</View>
);
}
}
You can open DrawerNavigation on button click like this.
<Button title="Menu" onPress ={ ( ) => this.props.navigation.openDrawer()} />
Don't put Stack into View. It's hard to understand and you break all props.
And the reason it doesn't work is that navigationOptions in second code is not for the drawer but for the StackNavigator in the first code. So it can't execute drawer's navigation.navigate("DrawerOpen") because it's StackNavigator's.
And I highly recommend you to change your app's hierarchy. It's really hard that child Drawer passes its navigation to parent Stack's right button.
Then, it would look like this.
const MyStack = StackNavigator({
Tabs:{ screen: MyTabs, navigationOptions:(props) => ({
headerRight:
<TouchableOpacity onPress={() => {props.screenProps.myDrawerNavigation.navigate('DrawerOpen')}}>
<Text>Open Drawer</Text>
</TouchableOpacity>
})}
}
, {navigationOptions:commonNavigationOptions})
const MyDrawer = DrawerNavigator({
stack1: {
screen: ({navigation}) => <MyStack screenProps={{myDrawerNavigation:navigation}} />,
},
stack2: {
//more screen
}
})