React Native Navigator: Custom component not visible - react-native

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.

Related

react native navigation error "navigate is undefined"

I am creating a react native application and I have problem when I want to navigate between screens it gives error as " Cannot read property 'navigate' of Undefined " . I want when I press gear icon it must navigate to settings part . I couldn't clearly see where did I mistake .
settings button touchable opacity
import React from 'react';
import {createStackNavigator} from 'react-navigation-stack';
import { createAppContainer} from 'react-navigation';
import PS from '../screens/PS';
import SPC from '../screens/SPC';
import TFA from '../screens/2FA';
import Login from '../screens/Login';
import Settings from '../screens/settings';
const screens = {
Login: {
screen: Login,
navigationOptions: {
header: null,
}
},
TFA:{
screen: TFA
},
PS: {
screen: PS,
navigationOptions: {
header: null,
}
},
SPC: {
screen: SPC
},
Settings: {
screen: Settings
}
}
const HomeStack = createStackNavigator(screens);
export default createAppContainer(HomeStack);
import React, {useState}from 'react';
import SettingsButton from './SettingsButton';
import ConnectionIcon from './Connectionİcon';
import { View, Image, StyleSheet } from 'react-native';
const Header = () => {
return (
<View style={styles.header}>
<View style={styles.headerp1}></View>
<View style={styles.headerp2}><Image
source={require('../assets/images/monitor.png')}
/></View>
<View style={styles.headerp3}>
<SettingsButton />
<ConnectionIcon />
</View>
</View>
)
}
export default Header;
// const styles here
import React from 'react';
import { Image, TouchableOpacity, StyleSheet } from 'react-native';
const SettingsButton = ({ navigation }) => {
return (
<TouchableOpacity
style={styles.headerp3v2}
onPress={() => navigation.navigate('Settings')}
>
<Image style={styles.reddot}
source={require('../assets/images/gear.png')}
/>
</TouchableOpacity>
)
}
export default SettingsButton;
// const stlyes codes here
header file is here
import { Image, StyleSheet, Button, View, Text, SafeAreaView, StatusBar } from 'react-native';
import Footer from '../components/Footer';
import Header from '../components/Header';
import PV from '../components/PV';
export default function PS() {
return (
<View style={styles.container}>
<StatusBar hidden />
<Header />
<View><Text style={styles.text}>All Persons</Text></View>
<PV />
<Footer/>
</View>
);
}```
You don't pass navigation prop into SettingsButton
You can try use useNavigation hook https://reactnavigation.org/docs/use-navigation/ for get navigation object into SettingsButton
import React from 'react';
import { Image, TouchableOpacity, StyleSheet } from 'react-native';
import { useNavigation } from '#react-navigation/native'; // <-- new code
const SettingsButton = () => {
const navigation = useNavigation(); // <-- new code
return (
<TouchableOpacity
style={styles.headerp3v2}
onPress={() => navigation.navigate('Settings')}
>
<Image style={styles.reddot}
source={require('../assets/images/gear.png')}
/>
</TouchableOpacity>
)
}
export default SettingsButton;

<TouchableOpacity onPress={() => navigation.navigate('Baslica')}> doesn't navigate any screen

I tried to give an onPress action to one of my custom button made with TouchableOpacity. It is supposed to navigate me to another screen. I did how exactly i did at other screens but this time it doesn't work and don't get any error as well. On the Navigation.js, when i give initialRouteName manually, screen appears, but when i click on the button, nothing happens.
Home Screen:
import React from "react";
import { StyleSheet, View, StatusBar, Image, ImageBackground, TouchableOpacity} from "react-native";
const HomeScreen = ({ navigation }) => {
return (
<View>
<TouchableOpacity onPress={() => navigation.navigate('Baslica')}>
<ImageBackground
source={require("../../assets/HomeScreen/baslicaButton.png")}
resizeMode="contain"
style={styles.baslicaButton}
imageStyle={styles.baslicaButton_imageStyle}
>
<Image
source={require("../../assets/HomeScreen/baslicaText.png")}
resizeMode="contain"
style={styles.baslicaText}
></Image>
</ImageBackground>
</TouchableOpacity>
</View>
);
}
export default HomeScreen;
Navigation JS:
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import IntroScreen from './src/screens/IntroScreen';
import HomeScreen from './src/screens/HomeScreen';
import BaslicaScreen from './src/screens/BaslicaScreen';
const navigator = createStackNavigator(
{
Intro: IntroScreen,
Home: HomeScreen,
Baslica: BaslicaScreen
},
{
initialRouteName: "Intro",
}
);
export default createAppContainer(navigator);
You need to wrap your components between AppContainer tags in your root component similar with below so that the navigation object become aware of the react-navigation context.
import AppContainer from './navigation'; // your navigation.js file
export default class RootApp extends React.Component {
...
render() {
return <AppContainer>
// the rest of your other components here
</AppContainer>
}
}
you Should use navigation param like this:
this.props.navigation.navigate("yourScreen", { ParamName: Valu });
Edit Your code like This:
import React from "react";
import { StyleSheet, View, StatusBar, Image, ImageBackground, TouchableOpacity} from "react-native";
const HomeScreen = ({ navigation }) => {
return (
<View>
<TouchableOpacity onPress={() =>
this.props.navigation.navigate('Baslica')}>
<ImageBackground
source={require("../../assets/HomeScreen/baslicaButton.png")}
resizeMode="contain"
style={styles.baslicaButton}
imageStyle={styles.baslicaButton_imageStyle}
>
<Image
source={require("../../assets/HomeScreen/baslicaText.png")}
resizeMode="contain"
style={styles.baslicaText}
></Image>
</ImageBackground>
</TouchableOpacity>
</View>
);
}
export default HomeScreen;
and Edit this code like that :
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import IntroScreen from './src/screens/IntroScreen';
import HomeScreen from './src/screens/HomeScreen';
import BaslicaScreen from './src/screens/BaslicaScreen';
const navigator = createStackNavigator(
{
Intro: {screen:IntroScreen},
Home: {screen:HomeScreen},
Baslica: {screen:BaslicaScreen},
},
{
initialRouteName: "Intro",
}
);
export default createAppContainer(navigator);

undefined is not an object(evaluating 'props.navigation.navigate')

////////////this is homescreen, here i try to go to 'Components'. it's not recognize 'props.navigation.navigate
import React from 'react';
import { Text, StyleSheet, View, Button, TouchableOpacity } from 'react-native';
const HomeScreen = props => {
return (
<View>
<Text style={styles.text}>Hi there !</Text>
<Button
onPress={() => props.navigation.navigate('Components')}
title="Go to Componetes demo"
/>
</View>
);
};
const styles = StyleSheet.create({
text: {
fontSize: 30
}
});
export default HomeScreen;
/////////This is the index.js here i am using homescreen.
import React from 'react';
import { AppRegistry,View } from 'react-native';
import HomeScreen from './src/screens/HomeScreen';
import ListScreen from './src/screens/ListScreen';
import ComponentsScreen from './src/screens/ComponentsScreen';
import Header from './src/screens/Header';
const App = () => {
return(
<View style={{ flex: 1 }}>
<Header headerText={'Hello ! '}/>
<HomeScreen />
</View>
);
};
AppRegistry.registerComponent('Tal', () => App);
This is the App.js,and the navigator is here. This is edited.
i Need help .///////////////////////////////////////////////
/////##########//////////
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* #format
* #flow
*/
import React, {Fragment} from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer } from 'react-navigation';
const navigator = createStackNavigator (
{
Home : HomeScreen,
Components : ComponentsScreen,
List : ListScreen
},
{
initialRouteName : 'Home',
defaultNavigationoptions : {
title : App
}
}
);
export default createAppContainer(navigator);
First, you need to understand how are you gonna have that navigation as prop inside a component.
Each screen component in your app is provided with the navigation prop automatically. It's important to highlight the navigation prop is not passed into all components; only screen components receive this prop automatically! React Navigation doesn't do anything magic here. For example, if you were to define a MyBackButton component and render it as a child of a screen component, you would not be able to access the navigation prop on it. If, however, you wish to access the navigation prop in any of your components, you may use the withNavigation HOC.
Please pass the navigation prop from the screen where you are introducing the homeScreen. It does not have access of navigation that is why it is throwing the error.

Icons not showing on Tab Navigator with 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!

How to change header title dynamically

I built a React Native app which has 2 tabs for change screen. When I change a screen I would like to change also the title of the header. So when I press Tab 1 the tile is Tab 1 and Tab 2 if Tab 2 screen is on.
I would like to understand what kind of change or add I need to do to perform that action in my code. Iùm sharing the code where I think should be done the change but if Iùm wrong please guide me to the right code I should share. I'm new to Native and I'm trying to learn it.
I have a Route.js file:
import React from 'react';
import { View, Platform, Image, StyleSheet } from "react-native";
import {createStackNavigator} from "react-navigation";
import FlightsTabNavigator from "./App/navigations/FlightsTabNavigator";
import FlightsHeader from "./App/components/header/FlightsHeader";
import HeaderStyle from "./App/styles/HeaderStyle";
import s from "./App/styles/headerImgStyle";
const Routes = createStackNavigator({
FlightsTabNavigator: {
screen: FlightsTabNavigator,
}
},{
initialRouteName: 'FlightsTabNavigator',
navigationOptions: {
headerTitle: '',
headerBackground: (
<Image
style={s.image}
//source={require('./App/assets/header/header.jpg')}
source={{uri: 'https://images.unsplash.com/photo-1503365113766-4a362681eac5?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=79cf794378a008ab1d74b8e612d72ad0&auto=format&fit=crop&w=1050&q=80'}}
/>
),
header: props => <FlightsHeader {...props} />,
...HeaderStyle,
animationEnabled: true
}
});
export default Routes;
And the Header:
import React from "react";
import { Header } from "react-navigation";
import { View, Text, Platform, Image, StyleSheet } from "react-native";
const FlightsHeader = props => {
return (
<View>
<Header {...props} />
</View>
);
};
export default FlightsHeader;