Load random screen w/ React Native Navigation - react-native

I'm trying to build an app in react native where you hit a button and it loads a random screen. Pretty simple, but I can't get the navigator working.
import React from 'react';
import {
AppRegistry,
Text,
View,
Button,
StyleSheet,
Image,
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import Slide0 from './slides/0';
import Slide1 from './slides/1';
import Slide2 from './slides/2';
randomScreen = Math.floor(Math.random() * 2);
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Home',
};
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<Image
source={require('./assets/icons/app-icon.png')}
/>
<Text style={{ marginTop: 100 }}>Welcome to Dementia Care Activities.</Text>
<Text style={{ marginTop: 100 }}>{randomScreen}</Text>
<Button
onPress={() => navigate('Slide0')}
title="Load Random Slide"
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
const SimpleAppNavigator = StackNavigator({
Home: { screen: HomeScreen },
Slide0: { screen: Slide0 }
});
const AppNavigation = () => (
<SimpleAppNavigator />
);
export default class App extends React.Component {
render() {
return (
<AppNavigation/>
);
}
}
So I'm trying to set randomScreen to a number, and that does work and I can see it in the , but I just cant figure out how to insert that number into
const SimpleAppNavigator = StackNavigator({
Home: { screen: HomeScreen },
Slide0: { screen: SlideINSERT-RANDOM-NUMBER }
});

You could just create a list of Screen objects. It may seem redundant since you are including integers to differentiate the Screen objects, but if you ever decide to change those object names to something different, the map will already have you covered. So something like:
const screens = [Screen0, Screen1]
Then:
const SimpleAppNavigator = StackNavigator({
Home: { screen: HomeScreen },
Slide0: { screen: screens[randomScreen] }
});

Related

react-native Drawer Navigation with Tab Navigation

I'm trying to set a top menu and a tab menu in my app. The bottom tabs are working, but the top menu is not displaying. The top menu that I am trying to set is the one we click and the left menu options show.
class App extends Component {
render() {
const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));
return (
<Provider store={store}>
<AppContainer
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
</Provider>
);
}
}
export default App;
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
}
}
class SettingsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
}
}
const TabNavigator = createBottomTabNavigator({
Home: HomeScreen,
Settings: SettingsScreen,
});
const MainStackNavigator = createStackNavigator({
Home: {
screen: TabNavigator
}
});
const AppDrawerNavigator = createDrawerNavigator({
Home:{
screen: MainStackNavigator
}
});
const AppSwitchNavigator = createSwitchNavigator({
Login: { screen: Login },
Main: { screen: AppDrawerNavigator }
});
const AppContainer = createAppContainer(AppSwitchNavigator);
I'm new with react native, so I am not sure the order to set the navigator options. What I'm doing wrong?
Thanks
I've put some code as an example with a MainDrawerNavigator that contains a MainTabNavigator. The tab navigator contains three stack navigators for this purpose I'll only reference the HomeScreenNavigator which is a stack navigator. By default this will show a stack header inside my Home tab but will not show a drawer icon to open the drawer. In order to do this you need to put an icon in to toggle the drawer. I've shown this by accessing the navigationOptions in the HomeScreen specifically shown:
static navigationOptions = (navData) => {
return {
headerLeft: (
<View style={styles.headerButtonLeft}>
<HeaderButtons HeaderButtonComponent={DefaultHeaderButton}>
<Item title="menu" iconName="ios-menu" onPress={() => {
navData.navigation.toggleDrawer()
}} />
</HeaderButtons>
</View>
),
}
}
The above will set an a header button on the left of the HomeScreen and pressing the Item will trigger toggling the drawer open via navData.navigation.toggleDrawer(). Your example would be similar except your outermost navigator would be your switch navigator it seems.
Full code as an example shown below: (let me know if clarification is needed elsewhere).
HomeScreen example:
class HomeScreen extends React.Component {
constructor(props) {
super(props)
this.state = { ... }
}
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
}
static navigationOptions = (navData) => {
return {
headerLeft: (
<View style={styles.headerButtonLeft}>
<HeaderButtons HeaderButtonComponent={DefaultHeaderButton}>
<Item title="menu" iconName="ios-menu" onPress={() => {
navData.navigation.toggleDrawer()
}} />
</HeaderButtons>
</View>
),
}
}
}
HomeScreenNavigator example:
import { createStackNavigator } from 'react-navigation-stack';
import HomeScreen from '../screens/HomeScreen';
import MediaSelectScreen from '../screens/MediaSelectScreen';
import FinalizePostScreen from '../screens/FinalizePostScreen';
import { userInterface } from '../constants/Colors';
import Styles from '../constants/Styles';
const HomeScreenNavigator = createStackNavigator({
Home: HomeScreen,
MediaSelect: MediaSelectScreen,
FinalizePost: FinalizePostScreen
}, {
defaultNavigationOptions: {
headerStyle: {
backgroundColor: userInterface.accentColor,
height: Styles.HEADER_HEIGHT,
},
}
});
export default HomeScreenNavigator;
MainTabNavigator example:
import { createBottomTabNavigator } from 'react-navigation-tabs';
import MessagesScreen from '../screens/MessagesScreen';
import HomeScreenNavigator from './HomeScreenNavigator';
import LearnScreenNavigator from './LearnScreenNavigator';
const MainTabNavigator = createBottomTabNavigator({
LearnScreen: { screen: LearnScreenNavigator, navigationOptions: () => ({
tabBarLabel: 'Learn'
})},
HomeScreen: { screen: HomeScreenNavigator, navigationOptions: {
tabBarLabel: 'Home'
}},
MessagesScreen: { screen: MessagesScreen, navigationOptions: {
tabBarLabel: 'Messages'
}},
});
export default MainTabNavigator;
MainDrawerNavigator example:
import { createDrawerNavigator } from 'react-navigation-drawer';
import { createAppContainer } from 'react-navigation';
import MainTabNavigator from './MainTabNavigator';
const MainDrawerNavigator = createDrawerNavigator({
DrawerNav: MainTabNavigator
}, {
drawerType: 'slide'
});
export default createAppContainer(MainDrawerNavigator);
App.js example:
import React from 'react';
import * as Font from 'expo-font';
import { AppLoading } from 'expo';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import AppSwitchNavigator from './navigation/AppSwitchNavigator';
export default class App extends React.Component {
state = { ... }
render() {
return (
<Provider store={store}>
<AppSwitchNavigator/>
</Provider>
);
}
}
const store = createStore(rootReducer);
const rootReducer = combineReducers({ ... });

React native, routing without a nav menu item

I have a drawer and bottom tab nav system on my app which works perfectly, but I"ve come to a point where I have modules where I want a button click to take the user to a page for data entry but I don't want that page on any nav bars, I just need to route to it on button press.
So in App.js i have my existing nav system:
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, Image } from 'react-native';
import Splash from './Splash';
import Login from './src/components/Login/Login';
import Dashboard from './src/components/Dashboard/Dashboard';
import Trends from './src/components/Trends/Trends';
import EnterWeight from './src/components/Dashboard/EnterWeight';
import Profile from './src/Profile';
import {createAppContainer, createSwitchNavigator} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';
import { createMaterialBottomTabNavigator } from 'react-navigation-material-bottom-tabs';
import { createDrawerNavigator } from 'react-navigation-drawer';
import Icon from '#expo/vector-icons/Ionicons';
import * as SQLite from 'expo-sqlite';
const db = SQLite.openDatabase('testDatabase');
const DashboardTabNavigator = createMaterialBottomTabNavigator({
Dashboard: {screen:Dashboard},
Trends: {screen:Trends},
//Profile: {screen:Profile},
EnterWeight: {screen:EnterWeight}
},
{
barStyle: { backgroundColor: '#000'},
},
{
navigationOptions:({navigation})=>{
const {routeName} = navigation.state.routes[navigation.state.index]
return{
headerTitle:routeName
}
}
}
);
const DashboardStackNavigator = createStackNavigator({
DashboardTabNavigator: DashboardTabNavigator
},
{
defaultNavigationOptions:({navigation}) => {
return {
headerLeft: (<Icon style={{paddingLeft:10}} onPress={()=>navigation.openDrawer()} name="md-menu" size={30} />),
headerRight: (<Image style={styles.crossLogo} source={require('./src/images/LOGO-cross_only.png')}/>)
}
}
});
const AppDrawerNavigator = createDrawerNavigator({
Dashboard:{screen:DashboardStackNavigator},
Logout:{screen:Login}
});
const AppSwitchNavigator = createSwitchNavigator({
Login:{screen: Login},
Dashboard:{screen:AppDrawerNavigator},
},
{
initialRouteName: 'Login',
});
export default class MMH_App_Final extends Component{
render() {
const App = createAppContainer(AppSwitchNavigator);
return(
<App />
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
crossLogo: {
width:30,
height:30,
marginRight:10
}
});
But on another page I have
<TouchableOpacity
style={styles.recordButton}
onPress={() => navigate('EnterWeight')}
>
I want to be able to navigate to EnterWeight without registering it in the navbar. How can I do that?
a simple way to do this is to manage order on your TabNavigator:
const DashboardTabNavigator = createMaterialBottomTabNavigator({
Dashboard: {screen:Dashboard},
Trends: {screen:Trends},
//Profile: {screen:Profile},
EnterWeight: {screen:EnterWeight}
},
{
barStyle: { backgroundColor: '#000'},
},
{
navigationOptions:({navigation})=>{
const {routeName} = navigation.state.routes[navigation.state.index]
return {
headerTitle: routeName,
order: [
'Dashboard',
'Trends',
//'Profile',
],
}
}
}
);
But you have other options like :
Create a custom TabNavigator
Reorder your stack implementation to
isolate this new screen

How to display the menu list from API in DrawerNavigator

I have created the side menu using DrawerNavigator with the static menu names and screens. I want the menu names and screens to be dynamic. Meaning to say I like to fetch those details in API and display in the DrawerNavigator. Please help to resolve this.
const MyApp = createDrawerNavigator({
HomeScreen: {
screen: HomeScreen,
},
WebMenuScreen: {
screen: WebMenuScreen,
},
}, {
drawerPosition: 'right',
},{
contentComponent: CustomDrawerComponent
}, {
initialRouteName: 'Login'
});
Try this, I hope this will helps you.
in Route File
import DrawerNavigator from "./DrawerNavigator";
import Home from './Home';
import About from './About'
import {
createStackNavigator,
createAppContainer,
} from "react-navigation";
const MainNavigator = createStackNavigator({
DrawerNavigator: {
screen: DrawerNavigator,
navigationOptions: {
header: null
}
},
Home: {
screen: Home,
navigationOptions: {
htitle: 'Home'
}
},
About: {
screen: About,
navigationOptions: {
htitle: 'About'
}
},
});
const Routes = createAppContainer(MainNavigator);
export default Routes;
in DrawerNavigator File
import React from 'react';
import { Platform, Dimensions } from 'react-native';
import { createDrawerNavigator, createAppContainer } from 'react-navigation';
import Home from './Home';
import About from '/About'
import MenuDrawer from './MenuDrawer';
const WIDTH = Dimensions.get('window').width;
const DrawerConfig = {
drawerWidth: WIDTH*0.83,
contentComponent: ({ navigation }) => {
return(<MenuDrawer navigation={navigation} />)
}
}
const DrawerNavigator = createDrawerNavigator(
{
Home:{
screen:Home
},
About:{
screen:About
},
},
DrawerConfig
);
export default createAppContainer(DrawerNavigator);
in Menu Drawer File
import React from "react";
import {
View,
Text,
ScrollView,
Image,
TouchableOpacity,
} from "react-native";
var obj = JSON.parse('{ "name":"Home","name":"About" }');
// import styles from './menuDrawerStyles'
class MenuDrawer extends React.Component {
navLink(nav, text) {
return (
<TouchableOpacity
style={{ height: 50 }}
onPress={() => this.props.navigation.navigate(nav)}
>
<Text style={styles.link}>{text}</Text>
</TouchableOpacity>
);
}
render() {
return (
<View style={styles.container}>
<ScrollView style={styles.scroller}>
{
this.obj.map((data) => {
<View style={styles.bottomLinks}>
<View style={{ flex: 2, flexDirection: "row" }}>
{this.navLink(data.name, data.name)}
</View>
<View style={{ flex: 2, flexDirection: "row" }}>
{this.navLink(data.name, data.name)}
</View>
</View>
})
}
</ScrollView>
</View>
);
}
}
in Menu Button File
import React from "react";
import { StyleSheet , Platform } from "react-native";
import Icon from "react-native-vector-icons/MaterialIcons";
export default class MenuButton extends React.Component {
render() {
return (
<Icon
name="reorder"
color="black"
size={25}
style={styles.menuIcon}
onPress={() => this.props.navigation.toggleDrawer()}
/>
);
}
}
const styles = StyleSheet.create({
menuIcon: {
zIndex: 9,
position: "absolute",
top: Platform.OS === "android" ? 15 : 25,
left: 20
}
});
In Menu Drawer file you can call Api and fetch menu list.

Question on Auth Flow and React Navigation (React Native)

I’m trying to setup the Auth flow with my app, but I can’t seem to figure it out exactly. Here’s how I’d like the flow to be, but please let me know if you have any repositories that might help for this:
Tabs:
Home
Search
Screens:
AuthLoading
Login
Home
Search
Search
Auth Flow:
App launches, checks for userToken
If userToken, redirect to Home
Screen (With Bottom Tab Bar)
If no userToken, redirect to Login Screen (Login Screen has Facebook OAuth from Expo)
User logins with Facebook and checks firebase If success, redirect to Home Screen (With Bottom Tab Bar) 4. If fail, redirect to Login Screen
router.js
import React from 'react';
import { Platform, StatusBar } from 'react-native';
import { createStackNavigator, createBottomTabNavigator, createSwitchNavigator } from 'react-navigation';
import { FontAwesome } from 'react-native-vector-icons';
import AuthLoadingScreen from '../screens/AuthLoadingScreen';
import LoginScreen from '../screens/LoginScreen';
import HomeScreen from '../screens/HomeScreen';
import SearchScreen from '../screens/SearchScreen';
export const UnauthenticatedStack = createStackNavigator({
AuthLoading: {
screen: AuthLoadingScreen,
navigationOptions: {
title: 'AuthLoading',
tabBarVisible: false,
header: null,
headerLeft: null,
headerRight: null,
},
},
Login: {
screen: LoginScreen,
navigationOptions: {
title: 'Login',
tabBarVisible: false,
header: null,
headerLeft: null,
},
},
});
export const AuthenticatedStack = createBottomTabNavigator(
{
Home: {
screen: HomeScreen,
navigationOptions: {
tabBarLabel: 'Home',
tabBarIcon: ({ tintColor }) => (
<FontAwesome name="home" size={24} color={tintColor} />
),
},
},
Search: {
screen: SearchScreen,
navigationOptions: {
tabBarLabel: 'Search',
tabBarIcon: ({ tintColor }) => (
<FontAwesome name="search" size={24} color={tintColor} />
),
},
},
},
);
export default createSwitchNavigator({
Home: AuthenticatedStack,
Login: UnauthenticatedStack,
},
{
initialRouteName: 'Home'
,
});
App.js
// Imports: Dependencies
import React from 'react';
import { View, StyleSheet } from 'react-native';
import firebase from 'firebase';
import { FirebaseAPIKey, authDomain, databaseURL, projectId, messagingSenderId } from './config/config';
import { UnauthenticatedStack, AuthenticatedStack } from './navigation/router';
// Firebase Config
export const firebaseConfig = {
apiKey: FirebaseAPIKey,
authDomain: `${authDomain}`,
databaseURL: `${databaseURL}`,
projectId: `${projectId}`,
// storageBucket: "",
messagingSenderId: `${messagingSenderId}`,
};
console.log(firebaseConfig)
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// React Native: Application
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<UnauthenticatedStack />
</View>
);
}
};
// Styles
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#CA3433',
},
});
AuthLoading.js
import React from 'react';
import { ActivityIndicator, AsyncStorage, StatusBar, StyleSheet, View, Text } from 'react-native';
export default class AuthLoadingScreen extends React.Component {
constructor(props) {
super(props);
this.checkUserToken();
}
async checkUserToken() {
const userToken = await AsyncStorage.getItem('userToken');
// If User Token
if (userToken) {
AsyncStorage.setItem(userToken);
this.props.navigation.navigate('Home');
}
else {
this.props.navigation.navigate('Login');
}
}
// Render any loading content that you like here
render() {
return (
<View style={styles.container}>
<Text style={styles.text}>Checking Authentication</Text>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
}
// Styles
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#CA3433',
justifyContent: 'center',
alignItems: 'center',
},
text: {
justifyContent: 'center',
color: '#fff',
fontSize: 18,
fontWeight: '500',
},
});
Home.js
// Imports: Dependencies
import React from 'react';
import { View, StyleSheet } from 'react-native';
// Imports: Components
import List from '../components/List';
// React Native Screen: Home
export default () => (
<View style={styles.container}>
<List />
</View>
);
// Styles
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
justifyContent: 'center',
alignItems: 'center',
},
});
Search.js
// Imports: Dependencies
import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';
// Imports: Components
// React Native Screen: Search
export default class Search extends Component {
// Render
render() {
return (
<View styles={styles.container}>
<Text>Search</Text>
</View>
)
}
}
// Styles
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
In App.js, do not export firebase config part.
App class should be like that:
export default class App extends React.Component {
componentWillMount() {
const firebaseConfig = {
apiKey: FirebaseAPIKey,
authDomain: `${authDomain}`,
databaseURL: `${databaseURL}`,
projectId: `${projectId}`,
// storageBucket: "",
messagingSenderId: `${messagingSenderId}`,
}
firebase.initializeApp(config);
}
render() {
return (
<View style={styles.container}>
<UnauthenticatedStack />
</View>
)
}
};
Move AuthLoadingScreen to createSwitchNavigator:
const rootNavigator = createSwitchNavigator({
Home: AuthenticatedStack,
Login: UnauthenticatedStack,
AuthLoading: AuthLoadingScreen,
},
{
initialRouteName: 'AuthLoading',
});
export const AppNavigation = createAppContainer(rootNavigator)
App.js
// React Native: Application
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<AppNavigation />
</View>
);
}
};

React Native navigation: undefined is not an object

I try to implement a navigation in React Native with react-navigation according to this tutorial, but I am facing the following error when launching the app:
undefined is not an object (Evaluating 'this.props.navigation.navigate')
render index.android.js:17:12
My index.android.js:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Button
} from 'react-native';
import {
StackNavigator,
} from 'react-navigation';
export default class Abc extends Component {
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Button
title="Go to List"
onPress={() =>
navigate('Liste')
}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
export class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Go to List"
onPress={() =>
navigate('Profile')
}
/>
);
}
}
export class Liste extends React.Component {
static navigationOptions = {
title: 'Liste',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Liste"
/>
);
}
}
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
});
AppRegistry.registerComponent('Abc', () => Abc);
Do you have any suggestions? Thank you!
I believe what you wanted to do is to register your App component in your AppRegistry instead of the Abc component.
The reason that you ran into this undefined is not an object (Evaluating 'this.props.navigation.navigate') is because the props.navigation is not available in your Abc component. If you look closely to the bottom of your code,
you have:
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
});
AppRegistry.registerComponent('Abc', () => Abc);
So you have created a StackNavigator component, but you are not registering this component but instead you are registering the Abc component in the AppRegistry, and since Abc component is not any of the screen under your StackNavigator, it won't have access to the this.props.navigation thus you would run into the exception.
You don't have Profile screen.
If you want to go to Liste screen from HomeScreen use:
onPress={() => navigate('Liste')}
If you want to go to Profile screen then you need to make Profile Screen and add it on StackNavigator, example:
export class Profile extends React.Component {
static navigationOptions = {
title: 'Profile',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Profile"
/>
);
}
}
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
Profile: {screen: Profile},
});
almost 3 years since the original question was asked. I had the same problem and landed on this page.
react-navigation is v5 now. The problem I had was because 'navigation' was not passing to the child component.
After a bit more research I fixed the problem using this solution from here.
I need to pass the props to child component like this.
<GoToButton navigation={props.navigation} />
https://reactnavigation.org/docs/connecting-navigation-prop
Hopefully this helps to new comers like me.