Accessing navigation parameters Expo-Go - reactjs

I have got "Screen1" where I create a string "blabla". According to the documentation, I can set it as a navigation parameter:
export default function Screen1() {
const navigation = useNavigation();
navigation.navigate("Screen2", { item: "blalbla" });
return (
<View>
<Text>Render some stuff</Text>
</View>
);
}
On Screen2 I should be able to access it with:
export default function Screen2({ route, navigation }) {
const { item } = route.params;
console.log(item);
return (
<View>
<Text>Render some stuff</Text>
</View>
);
}
Now, this returns:
TypeError: undefined is not an object (evaluating 'route.params.item')]
I also tried some other examples without success.
Referring to React-Native documentation: https://reactnavigation.org/docs/params/

I've created a Snack. Check this out.
Follow below steps to solve your problem
Create a Folder called navigation where your App.js is located.
Then inside this folder create a file called AppNavigator.js
Inside AppNavigator.js paste this
import React from 'react';
import { View } from 'react-native';
import { createStackNavigator } from '#react-navigation/stack';
import Screen1 from '../screens/Screen1';
import Screen2 from '../screens/Screen2';
const Stack = createStackNavigator();
function AppNavigator() {
return (
<Stack.Navigator>
<Stack.Screen name="Screen1" component={Screen1} />
<Stack.Screen name="Screen2" component={Screen2} />
</Stack.Navigator>
);
}
export default AppNavigator;
Now create another folder called screens where your App.js is located
Inside this folder create your two files Screen1.js and Screen2.js
They should look like this
Screen1.js -
import React from 'react';
import { View, StyleSheet, Text, Button } from 'react-native';
function Screen1({ navigation }) {
return (
<View style={styles.container}>
<Text>Screen 1</Text>
<Text onPress={() => navigation.navigate('Screen2', { item: 'blalbla' })}>
Press ME
</Text>
</View>
);
}
export default Screen1;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
Screen2.js -
import React from 'react';
import { View, StyleSheet, Text, Button } from 'react-native';
function Screen2({ route, navigation }) {
const { item } = route.params;
return (
<View style={styles.container}>
<Text>Screen 2</Text>
<Text onPress={() => navigation.navigate('Screen1')}>{item}</Text>
<Text onPress={() => navigation.navigate('Screen1')}>Press ME</Text>
</View>
);
}
export default Screen2;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
Now you should see route parameters inside Screen2

navigation object is probably not present in the screen
change Screen1({ navigation }) to Screen1(props) and do console.log(props) to check if you see a navigation object there

Related

How to navigate with react native expo?

I know it is a basic question but when I search on the web the answer is always a single file mith multiple functions link here for exemple.
What I want is to use one function per file and multiple file ( I fought it would be simple like in HTML but when I look into it it seems so complicated! I only want to navigate between 2 pages!)
The link which you provide we only need to do the changes that we have to create new file and paste that function in new file like below
HomeScreen.js file is looks like this
import * as React from 'react';
import { Button, View, Text } from 'react-native';
Const HomeScreen = ({ navigation }) => {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details')}
/>
</View>
);
}
export default HomeScreen;
DetailsScreen.js file is looks like this
import * as React from 'react';
import { Button, View, Text } from 'react-native';
Const DetailsScreen = ({ navigation }) => {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Button
title="Go to Details... again"
onPress={() => navigation.push('Details')}
/>
</View>
);
}
export default DetailsScreen;
And your app.js file looks like this.
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import HomeScreen from ‘../HomeScreen’;
import DetailsScreen from ‘../DetailsScreen’
const Stack = createNativeStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;

Navigating between screens in different files

I have been running into an issue with navigating between screens in different files; I am assuming this is due to improperly exporting the functional components from one file to another. I created a snack for it: https://snack.expo.dev/#figbar/exportingbetweenscreens, and pasted the code in that below. Essentially, if 'DetailsScreen' is in app.js, the project becomes the sample navigation project, and works properly, however, when it is in this new file, the details screen is blank when navigated to.
App.js:
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import {DetailsScreen} from './other';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.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>
);
}
export default App;
other.js:
import * as React from 'react';
import { Button, View, Text } from 'react-native';
function DetailsScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
export default DetailsScreen;
DetailsScreen was exported as default, so it should be imported like this:
import DetailsScreen from './other';
and not this:
import {DetailsScreen} from './other';
Remove the curly braces and you will be good to go. Or alternatively, don't export DetailsScreen as default

Why do I get "action navigate with payload undefined was not handled by any navigator"?

I am trying to do a small project myself to get to know React-Native.
I have 2 screens, and i want to navigate from my SplashScreen to HomeScreen by pressing on the button. But it gives me an error.
What should I add to get rid of the error?
Here are the codes
App.js
import 'react-native-gesture-handler'; // react-navigation, must be on top
import React from 'react';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import SplashScreen from "./src/screens/SplashScreen.js";
import HomeScreen from "./src/screens/HomeScreen.js";
const Stack = createStackNavigator();
const App = () => {
return (
// name is whatever you write
// initialRouteName is which screen you want as your initial
// when I add title, it will write it as title, not whats written in "name"
<NavigationContainer>
<Stack.Navigator initialRouteName="Intro" screenOptions={{title:"helllloo"}}>
<Stack.Screen name="Intro" component={SplashScreen} options={{title:"our intro"}} />
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
HomeScreen.js
import React from "react";
import { View, Text } from "react-native";
const HomeScreen = () => {
return(
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
)
}
export default HomeScreen;
SplashScreen.js
import React from "react";
import { Text, View, Button } from "react-native";
import HomeScreen from "./HomeScreen";
const SplashScreen = ({ navigation }) => {
// without flex:1, it will center on that row, not on whole page
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Hello</Text>
<Button
title="Go to HomeScreen"
onPress = {() => navigation.navigate(HomeScreen)}
/>
</View>
)
}
export default SplashScreen;
you define route here <Stack.Screen name="Home" component={HomeScreen} />
the route name is "Home"
and route component HomeScreen
to navigate call navigation.navigate(route name not route component)
so replace this navigation.navigate(HomeScreen) with navigation.navigate("Home")

React native open an activity

I am an Android developer who works with Android Studio most.
I started with my first react-native project for iOS and I want to know how is it possible to have one screen with a button that when the user clicks takes him to another screen (or activity in an android way) which has a hello message.
I would be really obliged to anyone that can help because I am really new to React and React-Native.
I have tried the following code but I get the following error.
My code is the following:
import React, { Component } from 'react';
import { StyleSheet, Text, View, Image, Button } from 'react-native';
import { createStackNavigator } from 'react-navigation';
import Settings from './Settings';
import Home from './Home';
const AppNavigator = createStackNavigator({
HomeScreen: { screen: Home },
SettingScreen: { screen: Settings },
});
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Image style={styles.image} source={require('./assets/adc.png')} />
<Text style={styles.adc}>Aratos Disaster Control</Text>
<Button title="Settings" onPress = {() => this.props.navigation.navigate('SettingScreen')} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
adc:{
fontWeight: 'bold',
marginTop: 20
},
image:{
width: 80,
height: 100
}
});
// Home.js
import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';
export class Home extends Component {
render() {
return (
<View>
<Text>This is the home screen</Text>
<Button title="Settings" onPress={() => this.props.navigation.navigate('SettingScreen')} />
</View>
)
}
}
export default Home
// Settings.js
import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';
export class Settings extends Component {
render() {
return (
<View>
<Text>This is the Settings screen</Text>
</View>
)
}
};
export default Settings;
For navigate between pages, you can use React Navigation or react-native-router-flux or other packages. You have to define the structure of your navigation and then navigate to each page using this packages. Read guides for more information.

React native stack navigation goBack() not working

I created common header for all pages, so create header component then i include that header in signup.js component goback navigation function working in signup.js page but not working header component getting undefined object navigate.goback error
Anyone give a solution for that like that i include component
sample code:
import React from 'react';
import { StyleSheet, Text, View, Image, ScrollView, TextInput,KeyboardAvoidingView, StatusBar } from 'react-native';
import {Icon,Body,Button,Title,Left,Right} from 'native-base';
import Expo from "expo";
import { Constants } from 'expo';
import Headerbar from "./../Header/header";
import Statusbar from "./../statusbar";
const googleIcon=require('../../../img/google_icon.png');
const fbIcon=require('../../../img/fb_icon.png');
export default class Socialsignup extends React.Component {
constructor(props) {
super(props);
this.state = {
};
}
render() {
return (
<ScrollView style={styles.socialContainer} scrollEnabled={true}>
<Statusbar/>
<Headerbar title="Social Login" />
<View style={styles.socialText}>
<Text style={styles.accountText}>Choose an account</Text>
</View>
<View style={styles.socialIconsBase}>
<View style={styles.socialIconsGoogle}>
<Image source={googleIcon} style={styles.googleIcon}/>
<Text style={styles.iconText}>
Google
</Text>
</View>
<View style={styles.socialIconsGoogle}>
<Image source={fbIcon} style={styles.fbIcon}/>
<Text style={styles.iconText}>
Facebook
</Text>
</View>
</View>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
socialContainer: {
backgroundColor: '#fff',
},
accountText: {
marginTop:25,
fontSize:20,
padding:20
},
socialIconsBase:
{
flexDirection:'row',
flex:1,
marginTop:40,
borderBottomWidth:1,
borderBottomColor:'#a8c0ce'
},
socialIconsGoogle:
{
flex:1,
alignItems:'center',
paddingTop:25,
paddingBottom:25,
borderRightWidth:1,
borderRightColor:'#a8c0ce',
marginBottom:8,
},
fbIcon:{
height:50,
width:50,
},
googleIcon:{
height:50,
width:50,
},
iconText:{
fontSize:15,
marginTop:15
}
});
you need to use this inside the header component :
import { withRouter } from 'react-router'
and then instead of export default HeaderClassName
use export default withRouter(HeaderClassName)
and you can go back by this.props.history.goBack()
You need to use this too:
<BrowserRouter>
<App />
and then add the pages like that
<Route path='/page' component={Page} />

Resources