React native navigation undefine object error - reactjs

I am facing a error called "undefine is not an object (evaluating '_this.props'). Its a very simple code which i am using the modern function of react native. I am not using class here.
my code is this App.js
import React from 'react';
import { StyleSheet, Text, View,TextInput,Button } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import Routes from './Routes';
export default function App() {
return (
<Routes/>
);
}
And Route.js
import React from 'react';
import { StyleSheet, Text, View,TextInput,Button } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import { render } from 'react-dom';
/// define all components here
import HomeScreen from './src/Component/HomeScreen';
import DetailScreen from './src/Component/DetailScreen';
// Routes defination
export default function Routes(){
const RootStack = createStackNavigator(
{
Home: HomeScreen,
Details: DetailScreen,
},
{
initialRouteName: 'Home',
}
);
const AppContainer = createAppContainer(RootStack);
return(
<AppContainer/>
);
}
And Homescreen.js from where i need this to be jump to detail screen
import React from 'react';
import { StyleSheet, Text, View,TextInput,Button } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
export default function HomeScreen() {
return (
<View>
<Text>I am testing from the home screen</Text>
<Button
title="Go to Details"
onPress={() => this.props.navigation.navigate('Details')}></Button>
</View>
)
}
And DetailScreen.js
import React from 'react';
import { StyleSheet, Text, View,TextInput,Button } from 'react-native';
export default function DetailScreen() {
return (
<View>
<Text>I am testing from the detail screen</Text>
</View>
)
}

Try withNavigation from react-navigation, you're seeing this error because you are not passing navigation from the parent.
import React from 'react';
import { StyleSheet, Text, View,TextInput,Button } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import {withNavigation} from 'react-navigation'
export default withNavigation(function HomeScreen() {
return (
<View>
<Text>I am testing from the home screen</Text>
<Button
title="Go to Details"
onPress={() => this.props.navigation.navigate('Details')}></Button>
</View>
)
})

Its fix
export default function HomeScreen(props) {
return (
<View>
<Text>I am testing from the home screen</Text>
<Button
title="Go to Details"
onPress={() => props.navigation.navigate('Details')}></Button>
</View>
)
}
because this.props only coming in class component but in function component its coming without a 'this'

Related

navigation.push is not a function. (In 'navigation.push("EditProfile")', 'navigation.push' is undefined) (React navigation V6)

I'm new to React Native and I'm trying to perform a simple stack navigation. I got it to work in another part of the application (User authentication step). My code then feeds onto another stack once the user is logged in. This stack navigator has a tab navigator nested in which may be causing the problem?
Either way I can't perform a push, from my profile screen to the edit profile screen. Code is below.
import React from 'react'
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import LoginScreen from './screens/LoginScreen';
import HomeScreen from './screens/HomeScreen';
import SignupScreen from './screens/SignupScreen';
import ProfileScreen from './screens/ProfileScreen';
import EditProfileScreen from './screens/EditProfileScreen';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import Icon from 'react-native-vector-icons/Ionicons';
const Stack = createNativeStackNavigator()
const screenOptions = {
headerShown: false
}
export const SignedOutStack = () =\> (
\<NavigationContainer\>
\<Stack.Navigator
initialRouteName="LoginScreen"
screenOptions={screenOptions}
\\>
\<Stack.Screen
name="Login"
component={LoginScreen}
/\>
\<Stack.Screen
name='SignupScreen'
component={SignupScreen}
/\>
\</Stack.Navigator\>
\</NavigationContainer\>
)
const Tab = createBottomTabNavigator();
export const SignedInStack = () =\> (
\<NavigationContainer\>
\<Tab.Navigator
screenOptions={screenOptions}\>
\<Tab.Screen name="Home" component={HomeScreen} /\>
\<Tab.Screen name="Profile" component={ProfileScreen} /\>
\</Tab.Navigator\>
\</NavigationContainer\>
)
export const ProfileStack = () =\> (
\<NavigationContainer\>
\<Stack.Navigator\>
\<Stack.Screen name="SignedInStack" component={SignedInStack} /\>
\<Stack.Screen name="EditProfile" component={EditProfileScreen} /\>
\</Stack.Navigator\>
\</NavigationContainer\>
)
Where I'm trying to implement the push
import { View, Text, SafeAreaView, ScrollView, StyleSheet, Image, TouchableOpacity, StatusBar, Button } from 'react-native'
import React, {useState, useEffect, useContext} from 'react';
import EditProfileScreen from './EditProfileScreen';
const ProfileScreen = ({navigation}) =\> (
// const {user, logout} = useContext(AuthContext)
<SafeAreaView style={styles.wrapper}>
<ScrollView
style={styles.container}
contentContainerStyle={{justifyContent: 'center', alignItems: 'center'}}
showsVerticalScrollIndicator = {false}
>
<Image style={styles.userImg} source={{uri: 'https://www.dmarge.com/wp-content/uploads/2021/01/dwayne-the-rock-.jpg'}} />
<Text style={styles.userName}>Person</Text>
<View style={styles.userBtnWrapper}>
<Button title="Edit Profile" onPress = {() => {
navigation.push("EditProfile")
}}/>
</View>
</ScrollView>
</SafeAreaView>
)
export default ProfileScreen
You may need to declare a stackAction before actually use navigation.
import { StackActions } from '#react-navigation/native';
....
const pushAction = StackActions.push('Profile', { user: 'Wojtek' });
navigation.dispatch(pushAction);

Problem with StackNavigator in reactnative

I am new to reactnative and I was following a tutorial with YouTube and I went exactly like an instructor, but my app does not work and the white screen shows
The program is very simple, there are three js files that I will put in now.
App.js :
import { StatusBar } from 'expo-status-bar';
import { Image, StyleSheet, Text, View, Button } from 'react-native';
import { NavigationContainer } from 'react-navigation';
import StackNavigator from './pages/StackNavigator';
export default function App() {
return (
<NavigationContainer>
<StackNavigator />
</NavigationContainer>
);
}
Home.js:
import { StatusBar } from 'expo-status-bar';
import { Image, StyleSheet, Text, View, Button } from 'react-native';
export default function App() {
return (
<View style={styles.container}>
<Image source={require('./header.png') } style={{ width: "100%", height: 300 }} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#04294F',
},
});
StackNavigator.js:
import { View, Text } from 'react-native'
import React from 'react'
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import Home from "./pages/Home";
const Stack = createNativeStackNavigator();
const StackNavigator = () => {
return (
<Stack.Navigator ScreenOptions={{headerShown: false }}>
<Stack.Group>
<Stack.screen name="Home" component={Home} />
</Stack.Group>
</Stack.Navigator>
);
};
export default StackNavigator;

react context in another screen

Good evening everyone,
I created two screens and I would like to pass the context to one of this screen but it doesn't work.
Here what I have so far :
App :
import React from 'react';
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
import HomeScreen from "./src/screens/HomeScreen";
import ExpenseScreen from "./src/screens/ExpenseScreen";
import BalanceScreen from "./src/screens/BalanceScreen";
import {ExpenseProvider} from "./src/context/ExpenseContext";
const navigator = createStackNavigator(
{
Home:HomeScreen,
Expense:ExpenseScreen,
Balance: BalanceScreen
},
{
initialRouteName:"Home",
defaultNavigationOptions:{
title:"App",
}
}
)
const App = createAppContainer(navigator);
export default () => {
return <ExpenseProvider>
<App/>
</ExpenseProvider>
};
HomeScreen:
import React from "react";
import { Text, StyleSheet, View, Button} from "react-native";
const HomeScreen = ({navigation}) => {
return(
<View>
<Text style={styles.text}>HomeScreen</Text>
{/*Shows default display*/}
<Button
onPress={()=> navigation.navigate('Expense')}
title="Expense Tracker"
/>
<Button
onPress={()=> navigation.navigate('Balance')}
title="Balance"
/>
</View>
)
};
const styles = StyleSheet.create({
text: {
fontSize: 30
}
});
export default HomeScreen;
Provider :
import React from 'react';
const ExpenseContext = React.createContext();
export const ExpenseProvider = ({children}) => {
const expensePosts = [
{title: 'Expense Post #1'},
{title: 'Expense Post #2'},
{title: 'Expense Post #3'}
]
return <ExpenseContext.Provider value={expensePosts}>{children}</ExpenseContext.Provider>
};
export default ExpenseContext;
Screen :
import React, {useContext} from "react";
import { Text, StyleSheet, View, Button, Flatlist } from "react-native";
import ExpenseContext from '../context/ExpenseContext';
const ExpenseScreen = () => {
const expensePosts = useContext(ExpenseContext)
return(
<View>
<Text style={styles.text}>Expense Tracker</Text>
<Flatlist
data={expensePosts}
keyExtractor={(expensePost) => expensePost.title}
renderItem={({item}) => {
return <Text>{item.title}</Text>
}}
/>
</View>
)
};
const styles = StyleSheet.create({
text: {
fontSize: 30
}
});
export default ExpenseScreen;
I am trying to import the context into the "expenseScreen" but it doesn't work.
The error message I have is " element type is invalid: expected a string or a class but got undefined. You likely forgot to export your component from the file it's defined in or you might have mixed up default and name imports. Check the render method of 'ExpenseScreen'".
What am I missing ?
Does the Expenseprovider exported in the App only applies for the 'homeScreen' ?
Thank you very much in advance and hope this is clear enough.

The navigation prop is missing for the navigator. in react-navigation you must set up your app container directly

im try to get this code i found online working locally in visual code studio, but it only works online
this is the expo to the code and all the file, https://snack.expo.io/#git/github.com/israsotomayor/react-native-redux-shopping-cart-count
and this is the link to my error:
shoppingCart.js file
import React, { Component } from "react";
import {
View,
Text,
StyleSheet
} from "react-native";
import { createStackNavigator } from 'react-navigation-stack'
import HomeScreen from './containers/HomeScreen'
import ElectronicsScreen from './containers/ElectronicsScreen'
import BooksScreen from './containers/BooksScreen'
import ShoppingCartIcon from './containers/ShoppingCartIcon'
import CartScreen from './containers/CartScreen'
class ShoppingCart extends Component {
render() {
return (
<AppStackNavigator />
);
}
}
export default ShoppingCart;
const AppStackNavigator = createStackNavigator({
Home: HomeScreen,
Electronics: ElectronicsScreen,
Books: BooksScreen,
Cart: CartScreen
}, {
navigationOptions: {
headerTitle: 'Shopping App',
headerRight: (
<ShoppingCartIcon />
)
}
})
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
}
});
app.js file
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import ShoppingCart from './ShoppingCart'
import { Provider } from 'react-redux'
import store from './store'
export default class App extends React.Component {
render() {
return (
<Provider store={store}>
<ShoppingCart />
</Provider>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
just checked and this structure should work properly :
import React, { Component } from "react";
import {
View,
Text,
StyleSheet
} from "react-native";
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack'
import HomeScreen from './containers/HomeScreen'
import ElectronicsScreen from './containers/ElectronicsScreen'
import BooksScreen from './containers/BooksScreen'
import ShoppingCartIcon from './containers/ShoppingCartIcon'
import CartScreen from './containers/CartScreen'
const AppStackNavigator = createStackNavigator({
Home: HomeScreen,
Electronics: ElectronicsScreen,
Books: BooksScreen,
Cart: CartScreen
}, {
navigationOptions: {
headerTitle: 'Shopping App',
headerRight: (
<ShoppingCartIcon />
)
}
})
export default createAppContainer (AppStackNavigator);
copy paste and it should work !!!

Passing Props for button on press event

I created a drawer (react-navigation) in Routes.js and I have a Button in HamburgerBtn.js. I wish to invoke the call on the button to open the drawer with my button. I do not understand how to get this even from outside of routes into the button.
#HamburgerBtn.js;import Hamburger from 'react-native-hamburger';
import React, {Component} from 'react';
import styles from './Component.style';
import {
Text,
View
} from 'react-native';
import {DrawerNavigator} from 'react-navigation';
import {NavigationActions} from 'react-navigation';
class HamburgerBtn extends Component {
constructor(props){
super(props);
this.state = {
active: false,
}
}
openControlPanel = () => {
this.props.navigation.navigate('DrawerOpen'); // open drawer
};
render () {
//onPress={()=> {this.setState({active: !this.state.active});this.props.navigation.navigate('DrawerOpen');}}/>
return (
<View style={styles.hamburgerBtnHome}>
<Hamburger active={this.state.active}
type = "arrow"
color = "black"
onPress={()=> {this.props.openControlPanel()}}
/>
</View>
);
}
}
export default HamburgerBtn;
--routes.js--
import Connect from './Connect/Connect';
import Setup from './Setup/Setup';
import Update from './Update/Update';
import homePage from './homePage'
import SideMenu from './SideMenu/SideMenu';
import {DrawerNavigator} from 'react-navigation';
openControlPanel = () => {
this.props.navigation.navigate('DrawerOpen'); // open drawer
};
export default DrawerNavigator({
homePage: {
screen: homePage
},
Connect: {
screen: Connect
},
Setup: {
screen: Setup
},
Update: {
screen: Update
}
}, {
contentComponent: SideMenu,
drawerWidth: 300
});
--homepage.js--
import React, {Component} from 'react';
import {
Text,
View
} from 'react-native';
import Hamburger from './Components/HamburgerBtn';
class homePage extends Component {
render () {
return (
<View style={{padding: 50}}>
<Hamburger/>
<Text>
HomePage
</Text>
</View>
);
}
}
export default homePage;
--hamburger--
import Hamburger from 'react-native-hamburger';
import React, {Component} from 'react';
import styles from './Component.style';
import {
Text,
View
} from 'react-native';
import PropTypes from 'prop-types';
import {NavigationActions} from 'react-navigation';
class HamburgerBtn extends Component {
constructor(props){
super(props);
this.state = {
active: false,
}
}
onPress = () => {
this.setState({active: !this.state.active});
this.props.onPress();
};
render () {
//onPress={()=> {this.setState({active: !this.state.active});this.props.navigation.navigate('DrawerOpen');}}/>
return (
<View style={styles.hamburgerBtnHome}>
<Hamburger active={this.state.active}
type = "arrow"
color = "black"
onPress={() => this.onPress()}
/>
</View>
);
}
}
export default HamburgerBtn;
--homepage--
import React, {Component} from 'react';
import {
Text,
View
} from 'react-native';
import Hamburger from './Components/HamburgerBtn';
class homePage extends Component {
openControlPanel = () => {
this.props.navigation.navigate('DrawerOpen'); // open drawer
};
render () {
return (
<View style={{padding: 50}}>
<Hamburger onPress={() => this.openControlPanel()} />
<Text>
HomePage
</Text>
</View>
);
}
}
export default homePage;

Resources