Icons not showing on Tab Navigator with React Native - react-native

I am having problems to show Icons using createBottomTabNavigator. I am using react-native-ionicons, and it works fine if I put any kind of Icon in the code, but is not showing on the tab bar. I already set showIcon: true.
Here is the code I am trying:
import React from 'react';
import {View, Text} from 'react-native';
import { createBottomTabNavigator, createAppContainer } from 'react-navigation';
import HomeScreen from './screens/HomepageScreen';
class SettingsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
}
}
const BottomNavigation = createBottomTabNavigator(
{
Home: HomeScreen,
Settings: SettingsScreen,
},
{
tabBarOptions: {
inactiveTintColor: 'gray',
swipeEnabled: true,
showLabel: true,
showIcon: true
},
}
);
export default createAppContainer(BottomNavigation);
Where HomepageScreen.js:
import React from 'react';
import {View, Text, Image, ScrollView} from 'react-native';
import {Card, CardItem, Right, Left} from 'native-base';
import {Header} from 'react-native-elements';
import Icon from 'react-native-ionicons'
class HomeScreen extends React.Component {
state = {
news: []
}
static navigationOptions = {
title: 'Noticias',
tabBarIcon: ({tintColor}) => {
<Icon style={{width: 10, height: 10, color: 'black'}} ios="ios-add" android="md-add" />
}
}
render() {
return (
//code
);
}
}
export default HomeScreen;
Am I missing something?

the size was probably too small, and you need to put color={tintColor} if you're going to put tintColor to use
import React from 'react';
import {View, Text, Image, ScrollView} from 'react-native';
import {Card, CardItem, Right, Left} from 'native-base';
import {Header} from 'react-native-elements';
import Icon from 'react-native-ionicons'
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Noticias',
tabBarIcon: ({tintColor}) => {
<Icon color={tintColor} ios="ios-add" android="md-add" />
}
}
render() {
return (
//code
);
}
}
export default HomeScreen;
hope it helps!

Related

Struggling with navigation after login - react native

Am facing a problem in navigating from Login screen to Dashboard in react-native and getting issue like TypeError: undefined is not an object (evaluating '_this.props.navigation.navigate') for more clarification, am attaching screenshot and images. Any help will be appreciable.
App.js
import React, {Component} from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import {createSwitchNavigator, createAppContainer} from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import {createDrawerNavigator} from 'react-navigation-drawer';
import { createBottomTabNavigator } from 'react-navigation-tabs';
import Icon from 'react-native-vector-icons/FontAwesome';
import WelcomeScreen from './screens/Welcome';
import LoginScreen from './screens/Login';
import DashboardScreen from './screens/Dashboard';
import SearchScreen from './screens/Search';
import ProfileScreen from './screens/Profile';
import BookingScreen from './screens/Booking';
import SupportScreen from './screens/Support';
const App: () => React$Node = () => {
return (
<AppContainer />
);
};
const DashboardTabNavigator = createBottomTabNavigator(
{
Search :{screen : SearchScreen,
navigationOptions:{
headerTitle: 'Search',
tabBarLabel:'Search',
tabBarIcon:({tintColor})=>(
<Icon name="search" color={tintColor} size={30}/>
)
}
},
Booking :{screen : BookingScreen,
navigationOptions:{
headerTitle: 'Booking',
tabBarLabel:'Booking',
tabBarIcon:({tintColor})=>(
<Icon name="list" color={tintColor} size={30}/>
)
}
},
Support :{screen : SupportScreen,
navigationOptions:{
headerTitle: 'Support',
tabBarLabel:'Support',
tabBarIcon:({tintColor})=>(
<Icon name="support" color={tintColor} size={30}/>
)
}
},
Profile :{screen : ProfileScreen,
navigationOptions:{
headerTitle: 'Profile',
tabBarLabel:'Profile',
tabBarIcon:({tintColor})=>(
<Icon name="user" color={tintColor} size={30}/>
)
}
},
},
{
tabBarOptions: {
showIcon: 'true', // Shows an icon for both iOS and Android
},
}
);
const DashboardStackNavigator = createStackNavigator({
Welcome :{screen : WelcomeScreen},
DashboardTabNavigator : DashboardTabNavigator
});
const AppSwitchNavigator = createSwitchNavigator({
Welcome :{screen : WelcomeScreen},
Dashboard :{screen : DashboardStackNavigator}
});
const AppContainer = createAppContainer(AppSwitchNavigator);
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
engine: {
position: 'absolute',
right: 0,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
color: Colors.dark,
},
highlight: {
fontWeight: '700',
},
footer: {
color: Colors.dark,
fontSize: 12,
fontWeight: '600',
padding: 4,
paddingRight: 12,
textAlign: 'right',
},
});
export default App;
Script for Welcome.js
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
Animated,
Button,
Dimensions,
TouchableOpacity,
TouchableHighlight,
Image,
ImageBackground,
ScrollView,
StatusBar,
Alert
} from 'react-native';
let { height, width } = Dimensions.get('window');
let InputWidth = width - ((width * 10) /100);
import Icon from 'react-native-vector-icons/FontAwesome';
import LoginScreen from './Login';
class WelcomeScreen extends Component{
constructor(props) {
super(props);
this.state = { isAnimationVisible: true, animationProgress: new Animated.Value(0) };
//this.LoginScreen = this.LoginScreen.bind(this);
}
componentDidMount() {
Animated.timing(this.state.animationProgress, { toValue: 1, duration: 5000 })
.start(() => this.setState(s => ({ ...s, isAnimationVisible: false })));
}
render() {
if (this.state.isAnimationVisible) {
const interpolation = this.state.animationProgress.interpolate({ inputRange: [0, 1], outputRange: [0, 1] });
return (
<View style={styles.backgroundColor}>
<Text style={styles.textAlignmentSplashHeader}>Text App</Text>
<View style={styles.textAlignmentSplashCenter}>
<Text style={styles.textAlignmentSplash}> <Icon name="check-square-o" color="#00bfff" size={20}/> Verified Provider</Text>
<Text style={styles.textAlignmentSplash}> <Icon name="check-square-o" color="#00bfff" size={20}/> Trained Professional</Text>
<Text style={styles.textAlignmentSplash}> <Icon name="check-square-o" color="#00bfff" size={20}/> Compare Prices</Text>
<Text style={styles.textAlignmentSplash}> <Icon name="check-square-o" color="#00bfff" size={20}/> At door step</Text>
</View>
</View>
);
}
else {
return (<LoginScreen />);
}
}
}
export default WelcomeScreen;
const styles = StyleSheet.create({
backgroundColor: {
flex: 1,
resizeMode: 'cover', // or 'stretch',
justifyContent: 'center',
backgroundColor: '#ffffff'
},
textAlignmentSplashHeader :{
textAlign:'center',
fontSize:35,
//color : '#70AB8F',
color : '#DC5B21'
},
textAlignmentSplashCenter :{
width: InputWidth,
margin : '20%',
justifyContent: 'center',
},
textAlignmentSplash :{
textAlign:'left',
fontSize:20,
color : '#808080'
},
});
Login.js
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
Button,
TouchableOpacity,
TouchableHighlight,
Image,
ImageBackground,
ScrollView,
StatusBar,
Alert
} from 'react-native';
import { Dashboard } from './Dashboard';
class LoginScreen extends Component{
static navigationOptions = {
header : 'Login'
}
constructor(props){
super(props);
this.state={
mobile : null
}
this.__login = this.__login.bind(this);
}
render(){
return(
<View style={{flex : 1, alignItems : 'center', justifyContent : 'center'}}>
<Text>LoginScreen</Text>
<Button
title ="Login"
onPress={this.__login}
/>
</View>
);
}
__login = ()=>{
this.props.navigation.navigate('Dashboard')
}
}
export default LoginScreen;
Dashboard.js
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
Button,
TouchableOpacity,
TouchableHighlight,
Image,
ImageBackground,
ScrollView,
StatusBar,
Alert
} from 'react-native';
class DashboardScreen extends Component{
render(){
return(
<View style={{flex : 1, alignItems : 'center', justifyContent : 'center'}}>
<Text>DashboardScreen</Text>
</View>
);
}
}
export default DashboardScreen;
And Below is error after clicking Login Button in Login Screen
below am getting error after clicking login button, navigation code is in Login.js
Please help me.
udpate your createSwitchNavigator
const AppSwitchNavigator = (signedIn = false)=>createSwitchNavigator({
Welcome :{screen : WelcomeScreen},
DashboardTabNavigator : DashboardTabNavigator
},
{
initialRouteName: signedIn ? 'DashboardTabNavigator' : 'Welcome',
},
);
const AppContainer = createAppContainer(AppSwitchNavigator(isLogin));
save isLogin in redux or context api when user login just update isLogin value to true and you will navigate to Dashboard.
You have to use WithNavigation in LoginScreen component like this:
import React, {Component} from 'react';
import { withNavigation } from 'react-navigation';
...
export default withNavigation(LoginScreen);
You are using LoginScreen as a component instead of a page and LoginScreen is not defined in your route definition so it has no access to this.props.navigation by default.

Unable to move to required screen using react-navigation?

I want to move to a different screen when I have some AsyncStorage item for authFlow but when I am going to the next screen and construction is calling out the function it is not moving to the next screen.
I tried to use the basic method this.props.navigation.navigate('navigator_name'), but i think it's not moving as blank screen is appearing.
App.js
------
import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { createSwitchNavigator, createAppContainer, } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import AuthLoadingScreen from './screens/auth-loading-screen';
import WelcomeScreen from './screens/welcomeScreen';
import AppHomeScreen from './screens/app-home-screen';
const AuthStackNavigator = createStackNavigator({
welcome: WelcomeScreen
})
// export default
const TestContainer = createAppContainer(createSwitchNavigator({
AuthLoading: AuthLoadingScreen,
Auth: AuthStackNavigator,
App: AppHomeScreen,
},
{
initialRouteName: 'AuthLoading',
}
))
export default class App extends React.Component {
constructor () {
super()
this.state = {
loggedIn: 'false',
}
}
render() {
return(
<View style={styles.container}>
<TestContainer />
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
auth-loading-screen
-------------------
import React, { Component } from 'react';
import { View, Text, StyleSheet, ActivityIndicator, AsyncStorage } from 'react-native';
export default class AuthLoadingScreen extends Component {
constructor (props) {
super(props);
console.log('calling _loadApp')
this._loadApp();
}
_loadApp = async() => {
const userToken = await AsyncStorage.getItem('userToken');
console.log('getting value of token ', userToken, JSON.stringify(this.props.navigation.actions))
this.props.navigation.navigate(userToken ? 'App' : 'Auth')
}
render() {
return (
<View style={styles.container}>
<ActivityIndicator />
</View>
)
}
}
const styles = StyleSheet.create ({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
})
welcomeScreen
-------------
import React, { Component } from 'react';
import { View, Text, StyleSheet, Button} from 'react-native';
export default class WelcomeScreen extends Component {
constructor (props) {
super(props);
}
render() {
return (
<View style={styles.container}>
<Text>Welcome</Text>
</View>
)
}
}
const styles = StyleSheet.create ({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
})
The screen should directly move to the welcomeScreen as item is not set through AsynStorage
Your function is asynchronous. However, the constructor cannot execute the asynchronous function. Therefore, you should change the position of executing the function.
async componentDidMount() {
await this._loadApp();
}

React Native Component is undefined

I want to create a new StackNavigator with 'react-navigation' package
but when I initialize the screens for the StackNavigator i get an error -> undefined is not an object (evaluating '_TextUpload.TextUpload')
My TextUpload Component:
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
export class TextUpload extends Component {
constructor(props){
super(props);
this.state = {
ready: false
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Text Upload
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
}
});
My StackNavigator Component:
import React from 'react';
import { createBottomTabNavigator, createAppContainer, createStackNavigator } from 'react-navigation';
import { Home, Profile, Feed, Upload, Notes, TextUpload, ImageScreen } from '../components';
import Icon from 'react-native-vector-icons/dist/FontAwesome';
import { createMaterialBottomTabNavigator } from "react-navigation-material-bottom-tabs";
import {HOME_TAB_COLOR, FEED_TAB_COLOR, UPLOAD_TAB_COLOR, NOTE_TAB_COLOR, PROFILE_TAB_COLOR} from "../config";
const AppNavigator = createStackNavigator({
Home: {
screen: TextUpload
}
});
You need import your pages on StackNavigator page. This is how i'm using;
import React, { Component } from "react";
import { createStackNavigator, createAppContainer } from 'react-navigation';
import Device from "../pages/device";
import DeviceScreen from "../pages/devices";
const Project= createStackNavigator({
Measures: {
screen: Device
},
Devices: {
screen: DeviceScreen
}
});
export default createAppContainer(Project);
its throw error when you import pages with curly brackets so don't use curly brackets.

How to import React native navigators?

I have defined all the navigators in a navigator.js file. I want to import it. How do I do that without getting any errors? This is my navigator.js file.
import React, {Component} from 'react';
import {
Platform,
View,
Button,
SafeAreaView,
} from 'react-native';
import {
createAppContainer,
createDrawerNavigator,
createBottomTabNavigator,
createStackNavigator,
DrawerItems,
} from 'react-navigation';
/** NAVIGATORS DEFINED **/
const WelcomeTabNavigator = createBottomTabNavigator({
Welcome: {screen: WelcomeScreen},
Profile,
Settings,
},
{
tabBarOptions: {
activeTintColor: '#fb9800',
inactiveTintColor: '#7e7b7b',
style: { height: 40,backgroundColor: '#fff',borderTopWidth:0.5,borderTopColor: '#7e7b7b' },
labelStyle: {fontSize: 20}
},
navigationOptions:({navigation}) => {
const {routeName} = navigation.state.routes[navigation.state.index];
return {
headerTitle: routeName
};
}
})
const WelcomeStackNavigator = createStackNavigator({
WelcomeTabNavigator: WelcomeTabNavigator
},
{
defaultNavigationOptions:({navigation}) => {
return {
headerLeft: (
<Icon
style={{paddingLeft: 20}}
onPress={() => navigation.openDrawer()}
name="md-menu"
size={30}
/>
)
};
}
})
const AppDrawerNavigator = createDrawerNavigator({
Welcome: {screen: WelcomeStackNavigator},
},
{
contentComponent:(props) => (
<View style={{flex:1}}>
<SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
<DrawerItems {...props} />
<Button
title="Logout"
onPress={() => {
props.navigation.navigate('Home')
}}
/>
</SafeAreaView>
</View>
),
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle'
})
const AppStackNavigator = createStackNavigator({
Home: {screen: HomeScreen},
Welcome: {screen: AppDrawerNavigator}
});
const AppContainer = createAppContainer(AppStackNavigator);
This is my app.js file
import React, {Component} from 'react';
import {
Platform,
View,
Button,
SafeAreaView,
} from 'react-native';
import {
createAppContainer,
createDrawerNavigator,
createBottomTabNavigator,
createStackNavigator,
DrawerItems,
} from 'react-navigation';
import Icon from 'react-native-vector-icons/Ionicons';
import HomeScreen from './screens/HomeScreen.js';
import WelcomeScreen from './screens/WelcomeScreen.js';
import Profile from './screens/ProfileScreen.js';
import Settings from './screens/SettingsScreen.js';
class App extends React.Component {
render() {
return <AppContainer />;
}
}
export default App;
This is my index.js file
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);
I want to know how to import the navigator.js file and connect these 3 files. My navigator.js file contains different navigators like tab navigator , drawer navigator, stack navigator etc. So how can I import all these in a single attempt?
You can combine all the navigators in one page, as long as you import all the pages.
import WelcomeScreen from './WelcomeScreen.js',
import ProfileScreen from './ProfileScreen.js',
import SettingsScreen from './SettingsScreen.js',
import LogoutScreen from './LogoutScreen.js',
const WelcomeTabNavigator = createBottomTabNavigator({
Welcome: {screen: WelcomeScreen,},
Profile: {screen: ProfileScreen,},
Settings: {screen: SettingsScreen,},
},
const WelcomeStackNavigator = createStackNavigator({
Home: {screen: WelcomeTabNavigator,},
Logout: {screen: LogoutScreen,},
})
class App extends React.Component {
render() {
return <WelcomeStackNavigator />;
}
}
export default App;
I like to tell you very simple way to update use navigation.
Below is an example of your index.js file
import React, { Component } from 'react';
import { StyleSheet, View } from 'react-native';
import { Main } from './navigation/Main';
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
});
export class App extends Component {
render() {
return (
<View style={ styles.container }>
<Main />
</View>
);
}
}
Then here is your Main.js which is your navigation class. You can declare all your screen here.
import {
createStackNavigator,
createAppContainer,
} from 'react-navigation';
import { FirstScreen } from '../screens/FirstScreen';
import { SecondScreen } from '../screens/SecondScreen';
const RootStack = createStackNavigator({
FirstScreen: {
screen: FirstScreen,
key: 'FirstScreen',
navigationOptions: {
gesturesEnabled: false,
},
},
SecondScreen: {
screen: SecondScreen,
key: 'SecondScreen',
navigationOptions: {
gesturesEnabled: false,
},
},
});
export const Main = createAppContainer(RootStack);
Now you can simply navigate from one screen to other.
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
TouchableOpacity,
} from 'react-native';
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,
},
});
class FirstScreen extends Component {
onGetStarted() {
const { navigate } = this.props.navigation;
navigate('SecondScreen');
}
render() {
return (
<View style={ styles.container }>
<TouchableOpacity
style={ styles.welcome }
onPress={ () => this.onGetStarted() }
>
<Text>
Welcome to React Native!
</Text>
</TouchableOpacity>
<Text style={ styles.instructions }>This is screen 1</Text>
</View>
);
}
}
export { FirstScreen };

React Native Navigator: Custom component not visible

My drawer has nothing displaying in it currently. If I get rid of the custom component it will display the Home option on the drawer. So I know there is something wrong with the way i'm setting up the custom component, but i'm not quite sure what since i'm not getting any errors and I have tried playing around with the style of the component.
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { createDrawerNavigator } from 'react-navigation';
import HomeScreen from './component/HomeScreen';
import Settings from './component/Settings';
import {Icon, Button, Container, Header, Content, Left} from 'native-base';
export default class App extends React.Component {
render() {
return (
<MyApp/>
);
}
}
const MyApp = createDrawerNavigator({
Home: {
screen: HomeScreen
}
}, {
initialRouteName: 'Home',
drawerPosition: 'right',
contentComponent: CustomDrawerContentComponent,
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle'
})
const CustomDrawerContentComponent = (props) => (
<Container>
<Header style={{height: 200, backgroundColor: 'white', paddingTop: 50}}>
<Body>
<Image style={{width: 30, height: 30}} source={require("./img/close.png")}/>
</Body>
</Header>
</Container>
);
You forgot to import Body from 'native-base' and Image from 'react-native'.
To fix your imports:
import {Icon, Body, Button, Container, Header, Content, Left} from 'native-base';
import {Image} from 'react-native';
Notice I removed the modules StyleSheet, Text and View from import { StyleSheet, Text, View } from 'react-native'; since they are not being used in your file.