How to acess my stacks from routes.js in App.js - react-native

How can i use AppContainer in my main file App.js? And navigate between pages of these two stacks AuthenticationNavigator and MainNavigator
I'm trying to acess my stacks from routes.js in the App.js, but i cant. Can someone tell me what's wrong in the code?
routes.js
const AuthenticationNavigator = createStackNavigator({
Login: {
screen: LoginScreen,
navigationOptions: {
title: 'Login',
headerShown: false, //desabilita a barra do topo
},
},
Register: {
screen: RegisterScreen,
navigationOptions: {
title: 'Cadastro',
},
},
}, {
initialRouteName: 'Login',
defaultNavigationOptions: {
headerTitleAlign: 'center',
headerTintColor: '#FFF',
headerStyle: {
backgroundColor: '#121212',
},
}
})
const MainNavigator = createStackNavigator({
Main: {
screen: MainScreen,
navigationOptions: {
title: 'Main',
headerShown: false,
},
},
}, {
initialRouteName: 'Main',
defaultNavigationOptions: {
headerTitleAlign: 'center',
headerTintColor: '#FFF',
headerStyle: {
backgroundColor: '#121212',
},
}
})
const AppNavigator = createSwitchNavigator({
/*
* Rather than being rendered by a screen component, the
* AuthenticationNavigator is a screen component
*/
Auth: AuthenticationNavigator,
Home: MainNavigator,
}, {
initialRouteName: 'Auth'}
);
const AppContainer = createAppContainer(AppNavigator);
export default AppContainer
The way it's imported in App.js
import AppContainer from "./src/routes"
Where and how i try to acess the stacks(App.js):
render() {
if (!this.state.isAuthenticationReady) {
return (
{}
);
} else {
return (
<>
<StatusBar barStyle="light-content" backgroundColor="#121212" />
{this.state.isAuthenticated ? <AppContainer /> : <AppContainer />}
</>
);
}
}

Make sure you are exporting your all routes like
export {InitialStack, MainStack, Loading}
because you want to use it as
import {InitialStack, MainStack, Loading} from "./src/routes"
And you need to add state in your this line
{this.isAuthenticated ? <MainStack /> : <InitialStack />} // change this to
{this.state.isAuthenticated ? <MainStack /> : <InitialStack />}

Related

Pass to props to all screen of topnavigations

I'm trying to send props to all the screen children but only the default screen get the props.
I want to send the props to all the screen of the top navigation. I tried to navigate directly to the tab but it doesn't work either . I don't know what i'm doing wrong.
class RestaurantSummary extends Component {
render () {
return (
<TouchableRipple
onPress={() => this.props.nav.navigation.navigate("Plat", { restaurant: this.props.restaurant,
token: this.props.token, uri: uri})}>
<Card style={styles.container}>
<Card.Cover source={{ uri: uri }} />
<Card.Content style={styles.content}>
<Text style={styles.Text}>{this.props.restaurant.name} - {this.props.restaurant.address} </Text>
</Card.Content>
</Card>
</TouchableRipple>
)
}
}
Here is how i built my navigations . I have a switch navigator which allows me to switch between the app stack and the home stack. And in the App stack i have top bar navigations.
const TabScreen = createMaterialTopTabNavigator(
{
Entrées: { screen: Entrées },
Plat: { screen: Plat },
},
{
tabBarPosition: 'top',
swipeEnabled: true,
animationEnabled: true,
tabBarOptions: {
activeTintColor: '#FFFFFF',
inactiveTintColor: '#F8F8F8',
style: {
backgroundColor: '#633689',
},
labelStyle: {
textAlign: 'center',
},
indicatorStyle: {
borderBottomColor: '#87B56A',
borderBottomWidth: 2,
},
},
}, {
initialRouteName: 'Plat',
}
);
const top = createStackNavigator({
TabScreen: {
screen: TabScreen,
navigationOptions: {
headerStyle: {
backgroundColor: 'transparent',
},
headerTintColor: '#FFFFFF',
title: 'TabExample',
},
},
});
const AuthStack = createStackNavigator({
SignIn: {
screen: SignIn
},
SignUp: {
screen: SignUp
},
SignUpBiss: {
screen: SignUpBiss
}
},{
navigationOptions: {
headerTransparent: true,
},
mode: 'modal',
headerBackTitleVisible: 'true'
})
const AppStack = createStackNavigator({
Home: {
screen: Home
},
tab: top,
RestaurantDetail: {
screen: RestaurantDetail,
navigationOptions:()=>{
return {
tabBarVisible:false,
};
}
},
})
AppStack.navigationOptions = ({ navigation }) => {
let tabBarVisible = true;
if (navigation.state.index > 0) {
tabBarVisible = false; }
return {
tabBarVisible,
}
}
const bottomNav = createBottomTabNavigator({
Home : AppStack,
Logout : {screen: Logout}
})
const Nav = createSwitchNavigator({
App: bottomNav,
Auth: AuthStack
}, {
initialRouteName: 'Auth',
})
export default createAppContainer(Nav)
I saw your problem and I feels your code is little bit in a chaos.
firstly you can change
onPress={() => this.props.nav.navigation.navigate("Plat",
to
onPress={() => this.props.nav.navigation.navigate("TabScreen",
and then try this code in your Tab Screens
this.props.navigation.dangerouslyGetParent().getParam('restaurant')
this.props.navigation.dangerouslyGetParent().getParam('token')
this.props.navigation.dangerouslyGetParent().getParam('uri')
Hope it helps .feel free for doubts

trying to display a hamburger menu in left corner to open left side drawer?

I need to display a hamburger menu in the upper left hand corner which opens a left side drawer when clicked. I tried to implement the example provided in the following article but it doesn't appear to be functional when I implement it:
https://medium.com/#mollyseeley/a-simple-react-native-navigation-drawer-ba10fc203ad
Can you identify the problem with the implementation at the url above, or am I most likely messing something up with how I'm trying to implement the example? Are there other example urls for this type of implementation that you can recommend?
Try using this way.
const LoginNavigator = createStackNavigator({
Login: {
screen: Login,
navigationOptions: {
header: null,
}
},
SignUp: {
screen: SignUp,
navigationOptions: {
header: null,
}
}
});
class NavigationDrawerStructure extends Component {
toggleDrawer = () => {
//Props to open/close the drawer
this.props.navigationProps.toggleDrawer();
};
render() {
return (
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity onPress={this.toggleDrawer.bind(this)}>
<Image
source={require('../Img/hamburger.png')}
style={{ width: 25, height: 25, marginLeft: 20, tintColor: '#ffffff' }}
/>
</TouchableOpacity>
</View>
);
}
}
const HomeActivity_StackNavigator = createStackNavigator({
Home: {
screen: Main,
navigationOptions: ({ navigation }) => ({
title: 'Dashboard',
headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
headerStyle: {
backgroundColor: '#FF9800'
},
headerTintColor: '#fff'
}),
},
}, {headerLayoutPreset: 'center'});
const DrawerNavigators = createDrawerNavigator({
//Drawer Optons and indexing
Main: {
screen: HomeActivity_StackNavigator,
navigationOptions: {
drawerLabel: 'Dashboard',
}
}
});
const NonDrawerNavigators = createStackNavigator({
TaskView: {
screen: TaskView,
navigationOptions: {
title: 'Task',
headerStyle: {
backgroundColor: '#FF9800',
},
headerTintColor: '#fff'
}
},
TeamView: {
screen: TeamView,
navigationOptions: {
title: 'Team',
headerStyle: {
backgroundColor: '#FF9800',
},
headerTintColor: '#fff'
}
}
}, {headerLayoutPreset: 'center'});
const AppNavigator = createStackNavigator({
drawerStack: {
screen: DrawerNavigators,
navigationOptions: {
header: null
}
},
nonDrawerStack: {
screen: NonDrawerNavigators,
navigationOptions: {
header: null
}
}
});
export default createAppContainer(createSwitchNavigator({
SplashScreen: SplashScreen,
loginStack: LoginNavigator,
homeStack: AppNavigator
}, {
initialRouteName: 'SplashScreen'
})
);

Default React Native header not going away on screen

I have a screen that is known as the Settings Screen and I don't want the default React Native header to be present on that screen, but it is. Here is the (applicable) code I have for the screen so far:
export default class SettingsScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
address: "",
}
this.getStore();
}
async getStore() {
try {
const secretsInCache = await AsyncStorage.getItem('data');
const jsonData = JSON.parse(secretsInCache);
this.setState({address: jsonData.base_info.address});
} catch (e) {
console.log(e);
}
}
static navigationOptions = {
header: null,
};
render() {
return (
<PageTemplate headerText='Settings' navBar='true' needsFocus='More'>
<View style={{width: '100%', height: '100%', alignItems: 'center'}}>
<HomeScreenContainer>
<View style={styles.firstContainerSeperation}>
<Text style={styles.firstContHeading}>Service Address:</Text>
<Text style={styles.textInFirstBox}>{this.state.address}</Text>
</View>
<View style={styles.straightLine} />
</HomeScreenContainer>
</View>
</PageTemplate>
);
}
}
And here is the Tab navigator code:
const SettingsStack = createStackNavigator({
Settings: {
screen: SettingsScreen,
headerMode: 'none',
mode: 'modal'
}
},
{
headerMode: 'none',
navigationOptions: {
headerVisible: false,
}
});
const MoreStack = createStackNavigator({
More: {
screen: MoreScreen,
mode: 'modal',
headerMode: 'none',
},
SettingsStack
});
MoreStack.navigationOptions = {
headerVisible: false,
tabBarVisible: false,
tabBarLabel: 'More',
tabBarIcon: <TabBarIcon source={require('../assets/icons/more.png')}/>,
};
As you can see, in multiple places I'm declaring the headerMode as either null or none, but there is still the default header popping up?
How can I get rid of the default React Native header in my Settings Screen?
Try this:
const SettingsStack = createStackNavigator({
Settings: {
screen: SettingsScreen,
navigationOptions: {
title: ' ',
headerBackTitle: ' ',
headerStyle: {
elevation: 0,
borderBottomWidth: 0,
backgroundColor: colors.white
}
}
}
})
And delete this of your SettingsScreen class:
static navigationOptions = {
header: null,
};
The most straightforward way is to set the header on static navigationOptions to null. I have downloaded the Playground example from React Navigation GitHub project and tested just that. The header disappeared.
Your Tab navigation code should look something like this:
const SettingsStack = createStackNavigator({
Settings: {
screen: SettingsScreen
},
More: {
screen: MoreScreen
},
}, {});
MoreStack.navigationOptions = {
headerVisible: false,
tabBarVisible: false,
tabBarLabel: 'More',
tabBarIcon: <TabBarIcon source={require('../assets/icons/more.png')}/>,
};
Make sure you're not having any real time building problems when you ctrl+s.
try to do in this way
`const SettingsStack = createStackNavigator({
Settings: { screen: SettingsScreen }
}, {
navigationOptions: {
header: null
}
})`
`const MoreStack = createStackNavigator({
More: { screen: MoreScreen }
}, {
navigationOptions: {
header: null
}
})`
`const MainStack = creatSwitchNavigatore({
SettingsStack : SettingsStack,
MoreStack : MoreStack
})

Cannot navigate to route from within navigation stack.

I have one file with my whole navigation stack. In my navigation header I have a menu and I want to open a Drawer. Now In this example I get the error: Cannot read property 'navigation' of undefined
My AppNavigation file:
import React from 'react';
import { Text } from 'react-native';
import { createStackNavigator, createDrawerNavigator } from 'react-navigation';
import Login from '../components/Login';
import Dashboard from '../components/Dashboard';
import NewNotification from '../components/NewNotification';
const GuestStack = createStackNavigator(
{
loginScreen: { screen: Login },
}, {
headerMode: 'float',
headerLayoutPreset: 'center',
navigationOptions: {
headerStyle: { backgroundColor: '#61b1cd' },
title: 'Welcome',
headerTintColor: 'black',
},
},
);
const LoggedinStack = createDrawerNavigator({
dashboard: { screen: Dashboard },
newNotifciation: { screen: NewNotification },
});
const LoggedinNavigation = createStackNavigator(
{
LoggedinStack: { screen: LoggedinStack },
}, {
headerMode: 'float',
navigationOptions: {
headerStyle: { backgroundColor: '#61b1cd' },
title: 'Welkom!',
headerTintColor: 'black',
headerLeft: <Text onPress = { () =>
this.props.navigation.openDrawer('dashboard')
// navigation.openDrawer('dashboard')
}>Menu</Text>,
},
},
);
const VveNavigator = createStackNavigator(
{
guestStack: {
screen: GuestStack,
},
loggedinStack: {
screen: LoggedinNavigation,
},
}, {
headerMode: 'none',
initialRouteName: 'guestStack',
},
);
export default AppNavigator;
The problem seems to be over here:
headerLeft: <Text onPress = { () =>
this.props.navigation.openDrawer('dashboard')
// navigation.openDrawer('dashboard')
}>Menu</Text>,
And then in my App.js I have
export default class App extends React.Component {
render() {
return (
<APPNavigator />
);
}
}
Version of react navigation is 2.18.1
Thanks
headerLeft doesn't receive navigation prop (check the source code). So if you'd like to use a navigation prop on press, you should consider to refactor your stack navigator config:
const LoggedinNavigation = createStackNavigator(
{
LoggedinStack: { screen: LoggedinStack },
}, {
headerMode: 'float',
navigationOptions: ({ navigation }) => ({ // here you get the navigation
headerStyle: { backgroundColor: '#61b1cd' },
title: 'Welkom!',
headerTintColor: 'black',
headerLeft: (
<Text
onPress={() => {
navigation.openDrawer()
}}
>
Menu
</Text>
),
}),
},
);
Check this issue for more options.

How to force drawer to be over header with react navigation

I saw other answers regarding this issue but I can't find solution.
I want to show drawer always above app screen and above header. Instead drawer is always bellow header.
What am I doing wrong here:
const AppNavigation = createStackNavigator(
{
Main: { screen: Main, navigationOptions: {
title: "Main"
} },
Home: { screen: Home, navigationOptions: {
title: "Home"
} }
},
{
initialRouteName: "Main"
}
);
const DrawerNavigation = createDrawerNavigator(
{
Home: Home,
Main: Main
},
{
initialRouteName: "Main"
}
);
App = createStackNavigator({
drawer: {
screen: DrawerNavigation,
},
app: {
screen: AppNavigation
}
}, {
initialRouteName: 'drawer',
navigationOptions: ({navigation}) => ({
headerStyle: {backgroundColor: '#a9a9a9'},
title: 'Welcome!',
headerTintColor: 'white',
headerLeft: <Text onPress={() =>
navigation.dispatch(DrawerActions.toggleDrawer())}>Menu</Text>
})
});
export default () => (
<View style={{ flex: 1 }}>
<App />
</View>
Our app is similar to this. You can make the drawer be over the header with react-navigation if it is the <Root /> component of your app (the one you import into index.js). For example:
export const Root = DrawerNavigator({
Tabs: {
screen: Tabs,
title: 'Tabs'
},
STACK1: {
screen: STACK1,
title: 'STACK1',
},
STACK2: {
screen: STACK2,
title: 'STACK2',
},
});
That will give you something like this: