How to change header title dynamically - react-native

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;

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);

Icon not rendering from Ionicon vector icons in header

Trying to render a menu icon in the upper left corner for a stack navigator nested inside a bottom tab navigator. I am using react-navigation-header-buttons library for aid in formatting the icon in the stack header. Basically, the title of the Item component "Menu" is showing rather than the icon I am attempting to use.
//HomeScreenNavigator.js
import React from 'react';
import { createStackNavigator } from 'react-navigation-stack';
import { HeaderButtons, Item } from 'react-navigation-header-buttons';
import HomeScreen from '../screens/HomeScreen';
import MediaSelectScreen from '../screens/MediaSelectScreen';
import FinalizePostScreen from '../screens/FinalizePostScreen';
import Colors from '../constants/Colors';
import CustomHeaderButton from '../components/HeaderButton';
const HomeScreenNavigator = createStackNavigator({
Home: { screen: HomeScreen, navigationOptions: {
headerTitle: 'Feed',
headerLeft: (
<HeaderButtons> HeaderButtonComponent={CustomHeaderButton}
<Item title="Menu" iconName="ios-menu" onPress={() => {}} />
</HeaderButtons>
)
}},
MediaSelect: MediaSelectScreen,
FinalizePost: FinalizePostScreen
}, {
defaultNavigationOptions: {
headerStyle: {
backgroundColor: Colors.accentColor
}
}
});
export default HomeScreenNavigator;
//HeaderButton.js
import React from 'react';
import { HeaderButton } from 'react-navigation-header-buttons';
import { Ionicons } from '#expo/vector-icons';
import Colors from '../constants/Colors';
const CustomHeaderButton = props => {
return <HeaderButton
{...props}
IconComponent={Ionicons}
iconSize={23}
color={Colors.iconSelectedOutline}
/>
};
export default CustomHeaderButton
No error messages are shown but no icon is shown either. Instead The header bar just has a headerLeft button with text "MENU" and then the Header title "Feed". screenshot-simulator
Line of code that was causing the issue:
<HeaderButtons> HeaderButtonComponent={CustomHeaderButton}
Needs to be:
<HeaderButtons HeaderButtonComponent={CustomHeaderButton}>

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.

Single screen application without bottom navigation bar

I've analyzed the needs of my application and decided I can more efficiently develop it with mobile friendly components on the web and present it via a web view in an app.
I need permissions to access camera and gallery (to take photos / videos, and upload photos / videos).
Basically, a single-screen app with a webview presenting the site with mobile-friendly components and the above permissions.
This is my current app.js:
import React from 'react';
import { Button, Text, View, TouchableOpacity, StyleSheet } from 'react-native';
import Ionicons from 'react-native-vector-icons/Ionicons';
import {
createStackNavigator,
createBottomTabNavigator,
createAppContainer,
} from 'react-navigation';
//import createStackNavigator, createBottomTabNavigator, createAppContainer in our project
import HomeScreen from './pages/HomeScreen';
import { Constants, Location, Camera, Permissions } from 'expo';
const ProfileStack = createStackNavigator(
{
//Definition of Navigaton from home screen
HomeScreen: { screen: HomeScreen },
},
{
//For React Navigation 2.+ change defaultNavigationOptions->navigationOptions
defaultNavigationOptions: {
//Header customization of the perticular Screen
headerStyle: {
backgroundColor: 'orange',
},
headerTintColor: '#FFFFFF',
title: '',
//Header title
},
}
);
const App = createBottomTabNavigator(
{
HomeScreen: { screen: HomeScreen },
},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
let IconComponent = Ionicons;
let iconName;
if (routeName === 'HomeScreen') {
iconName = `ios-information-circle${focused ? '' : '-outline'}`;
}
return <IconComponent name={iconName} size={25} color={tintColor} />;
},
}),
tabBarOptions: {
activeTintColor: 'orange',
inactiveTintColor: 'gray',
},
}
);
export default createAppContainer(App);
and './pages/HomeScreen':
//This is an example code for Bottom Navigation//
import React, {Component} from 'react';
//import react in our code.
import { Text, View, TouchableOpacity, StyleSheet, WebView } from 'react-native';
//import all the basic component we have used
export default class ProfileScreen extends React.Component {
//Profile Screen to show from Open profile button
render() {
return (
<WebView
source={{uri: 'https://mobilesite'}}
style={{marginTop: 20}}
/>
);
}
}
So far, the site opens on a single screen as expected, but there's a bottom navigation bar present; I'd also like to preferably hide the top bar if possible too as I've accounted for that in a mobile-friendly header on the mobile site as well.
Also, via the mobile web codebase, I'm utilizing <input type="file" /> for uploading. Is this compatible with React Native Permissions?
You're using createBottomTabNavigator() and passing the resulting navigator to your createAppContainer() which will create a bottom tab navigator.
If you pass a StackNavigator (via createStackNavigator() which you use for ProfileStack) to the createAppContainer() it'll render the stack as opposed to the bottom tab navigator which is what you want.
To remove the header altogether you can use headerMode: 'none' in the navigationOptions param.
You can see a basic version of what you want here (sans the lack of a header) in the React Navigation docs.