Navigation between screen in react native - reactjs

So I try to navigate between 2 screens. I created a button on the tab bar(right)
but the problem is that it always says that is undefined.
Here is the code:
static navigationOptions = {
headerTitleStyle: { alignSelf: 'center', },
title:'Oferte',
headerStyle: {
backgroundColor: '#BA272A',
},
headerRight: (
<View style={{paddingRight:11}}>
<Button
color="#ff5c5c" title="Tombola"
onPress={() => this.props.navigation( 'Post', { name: 'Jane'} )}
/>
</View>
),
headerTintColor: 'white',
headerTitleStyle: {
fontWeight: 'bold',
},
}

You can use react-native-router-flux (npm install --save react-native-router-flux)
just make one Navigator.js file and define each page you wanted to navigate.
import React from 'react';
import { Router, Scene } from 'react-native-router-flux';
import LaunchScreen from '../components/LaunchScreen.js';
import Feed from '../components/Feed.js';
const Navigator = () => {
return (
<Router>
<Scene key="root">
<Scene key="lauchscreen" component={LaunchScreen} hideNavBar initial />
<Scene key="feedscreen" type="reset" hideNavBar component={Feed} />
</Scene>
</Router>
);
};
export default Navigator;
now in your App.js file add this:
import Navigator from './src/Navigator.js';
export default class App extends Component<Props> {
render() {
return (
<Navigator />
);
}
}
now in your login.js when you click on login button write this:
import { Actions } from 'react-native-router-flux';
onLoginClick() {
Actions.feedscreen();
}
Thats it.. happy coding.

Looking at https://reactnavigation.org/docs/4.x/redux-integration/#setparams-from-your-screen, probably you should do:
static navigationOptions = ({navigation}) => ({
headerTitleStyle: { alignSelf: 'center', },
title:'Oferte',
headerStyle: {
backgroundColor: '#BA272A',
},
headerRight: (
<View style={{paddingRight:11}}>
<Button
color="#ff5c5c" title="Tombola"
onPress={() => navigation( 'Post', { name: 'Jane'} )}
/>
</View>
),
headerTintColor: 'white',
headerTitleStyle: {
fontWeight: 'bold',
},
}}
When using this inside a static property or method it will refer to the class itself and not the instance of the component.

using react-navigation v5 component base api :
import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
function DetailsScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Button title="navigate" onPress={()=> props.navigate("home")} />
</View>
);
}
function HomeScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Button title="navigate" onPress={()=> props.navigate("details")} />
</View>
);
}
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
or with old v4 api:
// Other code for HomeScreen here...
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
}
class DetailsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
}
const AppNavigator = createStackNavigator(
{
Home: HomeScreen,
Details: DetailsScreen,
},
{
initialRouteName: 'Home',
}
);
// Other code for App component here...
Inside these components you can asses navigation prop, which consists of helper functions for navigating.Some frequently used one are
naviagtion.navigate("screenName") //navigates to new screen
navigation.goBack() // go backs to previous screen
navigation.push("screen name") // navigates to new screen with stack animation . this only works for stackNavigator
navigation.pop()// navigates to previous screen with stack animation . this only works for stackNavigator

Related

I want to pass data to another screen by FlatList in react native

Hello guys am new on react native I want to build an app like news app with posts,I stack in how to pass the data in flatlist to another screen to open the screen with the post data i use V6 navigation Note I do the Navigation and the post touchable this is my code :
the import :
import { StyleSheet, Text, View ,Button ,FlatList,TouchableOpacity } from 'react-native';
import React,{useState} from 'react';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
Screen one :
function HomeScreen({navigation}) {
const [news,setnews]=useState([
{title:'React Native students can get the basic knowledge',by:'Chris brown',dateTime:'15 may',key:'1'},
{title:'whats happening here is that you are trying to',by:'charley',dateTime:'15 jan',key:'2'},
{title:'statuses. specifically, you have to look here:',by:'yami',dateTime:'15 jon',key:'3'},
{title:'screen you want to show, when the authentication status changes.',by:'maria',dateTime:'15 nov',key:'4'},
]);
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<FlatList
data={news}
renderItem={({item})=>(
<TouchableOpacity onPress={()=>navigation.navigate('Detials' ,item)}>
<Text style={{fontSize:15,}}>{item.title}</Text>
</TouchableOpacity>
)}
/>
</View>
);
}
screen two:
function DetialsScreen({navigation}) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>{navigation.getParam('title')}</Text>
</View>
);
}
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Detials" component={DetialsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
screenone :
<FlatList
data={news}
renderItem={({item})=>(
<TouchableOpacity onPress={()=> this.props.navigation.navigate('ScreenTwo' , item)}>
<Text style={{fontSize:15,}}>{item.title}</Text>
</TouchableOpacity>
)}
/>
at screentwo you can get using this.props.route.params
export default class ScreenTwo extends Component {
constructor(props, ctx) {
super(props, ctx);
this.state = {
news: this.props.route.params
};
}
render() {
const news = this.state.news
return (
<View style={{flex: 1, margin: 30}}>
<Text>Title is : {news.title}{'\n'}{'\n'}by {news.by}</Text>
<Button
style={{marginTop: 20}}
onPress={() => this.props.navigation.goBack()}
title="Back"
color="#841584"
/>
</View>
);
}
}
for snack you can try here : https://snack.expo.dev/#edoofx/example1111
im using class anyway :D

ReactNavigation 5.0 browser next/previous button not enable

I have created a project using React Native for Web and it is working fine in mobile but when I run into the web it does not enable the browser default next-previous button.
How to enable browser button and manage next previous in browser
// In App.js in a new project
import * as React from 'react';
import { View, Text,Button,Platform } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
const isWeb = Platform.OS === 'web';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => linkTo('/Details')} title="Go to Details Screen" />
</View>
);
}
function Details() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text style={{fontWeight:'100',fontSize:30}}>Detail's Screen</Text>
</View>
);
}
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="HomeScreen">
<Stack.Screen name="HomeScreen" component={HomeScreen} options={{ headerShown: !isWeb}} />
<Stack.Screen name="Setting" component={Setting} options={{headerShown:!isWeb}}/>
<Stack.Screen name="Details" component={Details} options={{headerShown:!isWeb}}/>
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
I think you have to use "navigation" object that is part of the params to the Home Screen. Have a look at below code
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.navigate('Details');} />
</View>
);
}

React-navigation change url but not view

I am using React Native for Web and implemented navigation in my app but I am not able to navigate next - previous with browser button,
The URL is changed in the address bar of the browser but not able to change the view.
I have used React-Navigation for both Web/Mobile, Anyone knows a better approach please guide me
//App.js
import * as React from 'react';
import { View, Text,Button,Platform } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<View style={{ marginVertical: 20 }}>
<Text style={{fontWeight:'100',fontSize:30}}>Home Screen</Text>
</View>
<Button
title="Go to Details Screen"
onPress={() => navigation.navigate('Details')}
/>
<View style={ {marginVertical:20}}>
<Button title="Go to Setting Screen" onPress={() => navigation.navigate('Setting')} />
</View>
</View>
);
}
const Stack = createStackNavigator();
function App() {
const linking = {
prefixes: ['myurlhere://'],
config: {
screens: {
Setting: 'Setting',
Details: 'Details',
},
},
};
return (
<NavigationContainer linking={linking} >
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} options={{ headerShown: !isWeb, headerLeft:()=> null }} />
<Stack.Screen name="Setting" component={Setting} options={{headerShown:!isWeb}}/>
<Stack.Screen name="Details" component={Details} options={{headerShown:!isWeb}}/>
</Stack.Navigator>
</NavigationContainer>
);
}
[![//Setting Screen
function Setting() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text style={{fontWeight:'100',fontSize:30}}>Setting Screen</Text>
</View>
);
}
//Details Screen
function Details() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text style={{fontWeight:'100',fontSize:30}}>Detail's Screen</Text>
</View>
);
}]
export default App;
I am also looking into it.
But here is where I am at and its working.
const linking = {
prefixes: ['http://localhost:19006/', 'mychat://'],
config: {
screens: {
Home: '',
Search: '?:id/Search',
}
},
};
return (
<AppContext.Provider value={userSettings}>
<NavigationContainer linking={linking}>
<StackContainer />
</NavigationContainer>
</AppContext.Provider>
);

I'm using navigation to navigate my react native app and I couldn't Paypass this issue. I did as the docs but nothing works for me

The problem is I'm trying to use the navigation option to add a header and right button to navigate me to another screen, but it keeps giving me this error: "navigation.navigate is not a function. (in navigation.navigate is undefined)
Here is my code:
import 'react-native-gesture-handler';
import * as React from 'react';
import{View,Text, Button} from 'react-native'
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
function HomeScreen(navigation) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button title="button"
onPress={() => navigation.navigate('DetailsScreen')}></Button>
</View>
);
}
function DetailsScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Button
title="Go to Details... again"
onPress={() => navigation.navigate('Details')}
/>
</View>
);
}
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Homes" component={HomeScreen} />
<Stack.Screen name="DetailsScreen" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
You should change the definition of HomeScreen component
function HomeScreen(navigation)
To:
function HomeScreen({navigation})

React Native Navigation (ver4.x) unable to get props in custom header component. Keep getting undefined error

I have a custom header in my app and I am trying to get the navigation to open the Drawer when clicked on the menu item in the header component. I've passed the navigation and the header text as props to the component. However the props are being returned as undefined.
This is my header
import React from 'react';
import {View, StyleSheet, Text, Image, Alert, TouchableOpacity } from 'react-native';
import Colors from '../constants/Colors';
import { MaterialIcons } from '#expo/vector-icons';
const Header = ({navigation, headerText}) => {
const openMenu = () => {
console.log({headerText}) **/// this prints Object { "headerText": undefined,}**
navigation.openDrawer() **/// this also throws an undefined error**
}
console.log(headerText)**/// this displays the headerText correct on loading**
return (
<View style={styles.header} >
<MaterialIcons onPress={openMenu} name='menu' size={30} style={styles.icon} />
<Text style={styles.logo}>My Home Page Header</Text>
</View>
);
};
const styles = StyleSheet.create({
header: {
paddingTop: 20,
width: '100%',
height: '10%',
flexDirection: 'row',
},
logo: {
height: '100%',
width: '90%',
fontFamily: 'pacifico-regular',
fontSize: 28,
paddingTop: 20,
paddingLeft: 20,
},
icon: {
marginTop:30,
paddingLeft: 10,
},
});
export default Header;
console.log(headerText)/// this displays the header text correctly when loading the component.
However trying to use the props in the Text or the View throws an undefined error.
Clicking on the MaterialIcon menu icon gives the error :
Object {
"headerText": undefined,
}
and
TypeError: undefined is not an object (evaluating 'navigation.openDrawer')
This is the homestack.js
import { createStackNavigator } from 'react-navigation-stack';
import NewFeaturedRecipes from '../screens/NewFeaturedRecipes';
import NewRecipeDetails from '../screens/NewRecipeDetails';
import Header from '../shared/Header';
import React from 'react';
import {navigation} from 'react-navigation'
// const navigation = navigation;
const screens = {
NewFeaturedRecipes: {
screen: NewFeaturedRecipes,
navigationOptions: ({navigation}) => {
return {
headerTitle: () => <Header navigation={navigation}
headerText='Testting headertext from hoomestack' />
}
}
},
NewRecipeDetails: {
screen: NewRecipeDetails,
navigationOptions: {
title: 'Recipe Details',
}
},
}
const HomeStack = createStackNavigator(screens, {
defaultNavigationOptions: {
headerStyle: {
height: 20,
},
}
});
export default (HomeStack);
Here the answer if anybody is interested. I've used React Navigation 5.x for the nested navigators and I am able to call the openDrawer() from the menu in the Header component.
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, Button, DrawerLayoutAndroid } from 'react-native';
import { NavigationContainer, useNavigation } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import {createDrawerNavigator} from '#react-navigation/drawer';
import { MaterialIcons } from '#expo/vector-icons';
const Header = ({ headerText }) => {
const navigation = useNavigation()
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', flexDirection: 'row' }}>
<MaterialIcons onPress={()=> navigation.openDrawer()} name='menu' size={30} style={styles.icon} />
<Text>Header Text = {headerText}</Text>
</View>
)
}
const HomeScreen = ({ navigation }) => {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button onPress={() => navigation.navigate('Details')} title='Goto Details Screen' />
</View>
)
}
const DetailScreen = ({ navigation }) => {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Detail Screen</Text>
<Button onPress={() => navigation.navigate('Home')} title='Goto Home Screen' />
</View>
)
}
const HomeStack = () => {
return (
<Stack.Navigator>
<Stack.Screen
name='Home'
component={HomeScreen}
options={{
headerTitle: props => <Header headerText='Text from App' />
}}
/>
<Stack.Screen name='Details' component={DetailScreen} options={{
headerTitle: props => <Header headerText='Text from Details' />
}}
/>
</Stack.Navigator>
)
}
function NotificationsScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', flexDirection:'column' }}>
<Text>Notifications Screen</Text>
<Button onPress={() => navigation.goBack()} title="Go back home" />
</View>
);
}
const Stack = createStackNavigator()
const Drawer = createDrawerNavigator()
const App = () => {
return (
<NavigationContainer>
<Drawer.Navigator initialRouteName='Home'>
<Drawer.Screen name='Home' component={HomeStack}/>
<Drawer.Screen name='Notifications' component={NotificationsScreen}/>
</Drawer.Navigator>
</NavigationContainer>
)
}
export default App
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});

Resources