I want to push a screen but its not working:
TypeError: navigation.push is not a function. (In 'navigation.push('Restaurants', {
name: params
})', 'navigation.push' is undefined)
Explore.tsx
import { useNavigation } from '#react-navigation/core';
import { NativeStackScreenProps } from '#react-navigation/native-stack';
import React from 'react';
import { StyleSheet, Text, View, Pressable, ScrollView } from 'react-native';
import { RootStackParams } from '../../App';
import Card from '../components/Card';
type PropNav = NativeStackScreenProps<RootStackParams, 'Explore'>;
const Explore = ({ navigation }: PropNav) => {
const handleNavigate = (params: string) => {
navigation.push('Restaurants', { name: params });
};
return (
<View style={{marginTop: 200}}>
<ScrollView>
<Card name="Zum Profil" onPress={(params) => handleNavigate(params)} />
</ScrollView>
</View>
)
};
export default Explore;
App.tsx
import { StatusBar } from 'expo-status-bar';
import { useState } from 'react';
import { FlatList, StyleSheet, Text, View } from 'react-native';
import Explore from './src/screens/Explore';
import { NavigationContainer, NavigatorScreenParams } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import Profile from './src/screens/Profile';
import Restaurants from './src/screens/Restaurants';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import ProfileIcon from './src/icon/ProfileIcon';
export type RootStackParams = {
Explore: undefined;
Profile: undefined;
RestaurantsStack: NavigatorScreenParams<RestaurantTypesParms>;
Restaurants: {
name: string
}
}
const RootStack = createBottomTabNavigator<RootStackParams>();
export type RestaurantTypesParms = {
Restaurants: {
name: string
}
}
const RestaurantStack = createNativeStackNavigator<RestaurantTypesParms>();
const RestaurantScreen = () => {
return ( <RestaurantStack.Navigator>
<RestaurantStack.Screen name="Restaurants" component={Restaurants} />
</RestaurantStack.Navigator>)
};
export default function App() {
const [text, setText] = useState('');
return (
<NavigationContainer>
<RootStack.Navigator initialRouteName='Explore'
screenOptions={{
headerShown: false,
tabBarActiveTintColor: 'purple'
}}
>
<RootStack.Screen name="Explore" component={Explore}
options={{
tabBarIcon: ({ color, size }) => <ProfileIcon color={color} size={size} />,
tabBarLabel: "Explore"
}}
/>
<RootStack.Screen name="Profile" component={Profile} />
<RootStack.Screen name="RestaurantsStack" component={RestaurantScreen} />
</RootStack.Navigator>
</NavigationContainer>
);
}
But if I use navigation.navigate then it works....
........................................................................................................................................................................................................................................
The push method for the navigation prop is added additional for the StackNavigator. You can compare this with the official documentation.
The stack navigator adds the following methods to the navigation prop:
push
Pushes a new screen to top of the stack and navigate to it. The method accepts following arguments:
name - string - Name of the route to push onto the stack.
params - object - Screen params to pass to the destination route.
You are not using a Stack.Navigator. You need to install #react-navigation/native-stack and use a different navigator to make this work. I would suggest that you go through this documentation where a full working example is provided.
you can try this.
const handleNavigate = (params: string) => {
navigation.navigate('Restaurants', { name: params });
};
or
const handleNavigate = (params: string) => {
navigation.replace('Restaurants', { name: params });
};
or
const handleNavigate = (params: string) => {
navigation.navigate('RestaurantScreen', { name: params });
};
Related
I'm learning react native but I'm having difficulty with navigation, it's returning the error that navigation has not been initialized yet.
I looked for some tutorials, I tried some other ways, I went to the react native navigation documentation and, incredible as it may seem, it's the same as in the documentation... not even the GPT chat haha it didn't help me.
Can someone with experience in react native give me a light?
app.tsx:
import { NavigationContainer } from '#react-navigation/native';
import { createAppContainer } from 'react-navigation';
import StackNavigator from './app/index/navigator';
const AppContainer = createAppContainer(StackNavigator);
const App = () => {
return (
<NavigationContainer>
<AppContainer />
</NavigationContainer>
);
}
export default App;
navigator.tsx?
import { createStackNavigator } from 'react-navigation-stack';
import Index from '.';
import AddNewGrocery from '../components/addNewGrocery'
const StackNavigator = createStackNavigator({
home: { screen: Index, navigationOptions: { headerShown: false } },
addNewGrocery: { screen: AddNewGrocery, navigationOptions: { headerShown: false } },
});
export default StackNavigator;
index.tsx:
const Index = () => {
return (
<View style={styles.container}>
<Text style={styles.title}>Gestão de Compras</Text>
<LastFiveGrocery />
<MonthAverageSpend />
<TotalSpend />
<AddButton />
<StatusBar
translucent={false}
backgroundColor={'rgba(43, 43, 43, 1)'}
barStyle='light-content' />
</View>
);
}
AddButton.tsx:
import React from 'react';
import { StyleSheet, View, TouchableOpacity } from 'react-native';
import { Ionicons } from '#expo/vector-icons';
import { useNavigation } from '#react-navigation/native';
const AddButton = () => {
const navigation = useNavigation();
const handleAddButtonPress = () => {
navigation.navigate('addNewGrocery' as never);
}
return (
<TouchableOpacity style={styles.addButtonContainer} onPress={handleAddButtonPress}>
<View style={styles.addButton}>
<Ionicons name="ios-add" size={36} color="white" />
</View>
</TouchableOpacity>
);
}
I already tried to use it this way:
AddButton:
const { navigate } = useNavigation<StackNavigationProp<ParamListBase>>();
const handleAddButtonPress = () => {
navigate('addNewGrocery');
}
I've also tried using it this way:
navigator:
const StackNavigator = createAppContainer(createStackNavigator({
Home: { screen: Index },
addNewGrocery: AddNewGrocery,
}));
app.tsx:
import StackNavigator from './app/index/navigator';
const App = () => {
return (
<StackNavigator />
);
}
export default App;
You are using 2 different navigation library in simultaneously:
#react-navigation
react-navigation
Remove react-navigation and refactor the App.js file as below:
import { NavigationContainer } from '#react-navigation/native';
import StackNavigator from './app/index/navigator';
const App = () => {
return (
<NavigationContainer>
<StackNavigator />
</NavigationContainer>
);
}
export default App
StackNavigator should be implemented as per documentation -
https://reactnavigation.org/docs/stack-navigator/#api-definition
I'm getting this error when trying to import a font with expo font.
null is not an object" (evaluating 'dispatcher.useState')
But the error is for sure related to my font import.
import { NavigationContainer, useNavigation } from '#react-navigation/native';
import { FontAwesome } from '#expo/vector-icons';
import { MaterialCommunityIcons } from '#expo/vector-icons';
import {createStackNavigator} from '#react-navigation/stack';
import { AuthContext } from "./context";
import BottomTabNavigator from "./navigation/tabsNav";
import * as firebase from "firebase";
import Myapp from './instanceFirebase';
import AuthStackScreen from './navigation/authStack';
import { ActivityIndicator } from 'react-native'
import FlashMessage from "react-native-flash-message";
const instanceFirebase = Myapp;
import "firebase/functions";
import "firebase/firestore";
import * as Font from 'expo-font';
console.log("font", Font)
const [fontState, setFontState] = React.useState(false);
const MainStack = createStackNavigator();
const MainStackScreen = () => {
return(
<MainStack.Navigator initialRouteName="mainPage" >
<MainStack.Screen
name="mainPage"
component={BottomTabNavigator} />
</MainStack.Navigator>
)
}
const RootStack = createStackNavigator();
const RootStackScreen = ({ userToken }) => (
<RootStack.Navigator headerMode="none">
{userToken ? (
<RootStack.Screen
name="App"
component={MainStackScreen}
/>
) : (
<RootStack.Screen
headerMode="none"
name="Authstack"
component={AuthStackScreen}
options={{
animationEnabled: false
}}
/>
)}
</RootStack.Navigator>
);
export default function token() {
const [userToken, setUserToken] = React.useState(null);
const Context = {
instanceFirebase: instanceFirebase,
signOut: () => {
setUserToken(null);
},
signIn:() => {
setUserToken(true);
}
}
React.useEffect(() => {
const fetchFont = async () => {
await Font.loadAsync({
Jost: require('./assets/fonts/Jost-Regular.ttf'),
Jost_black: require('./assets/fonts/Jost-Black.ttf')
})
setFontState(true)
}
fetchFont()
firebase.auth().onAuthStateChanged(user => {
if(user){
setUserToken(true)
} else {
setUserToken(null)
}
})
}, [])
if (!fontState) {
return <ActivityIndicator />
} else {
return (
<AuthContext.Provider value={instanceFirebase}>
<NavigationContainer>
<RootStackScreen userToken={userToken} />
<FlashMessage position="top" style={{zIndex:99, flex:1, marginTop:30}}/>
</NavigationContainer>
</AuthContext.Provider>
)
}
}
I checked that expo-font is well import, and it is fine.
I don't understand what is happening, when i comment the code related to the import of the font my app just work. So my error is for sure related to the font import.
I do things correctly ?
Thanks per advance for your help
The right answer from Med El Mobarik:
Indeed i am not supposed to use the useState outside a funcionnal component
const [fontState, setFontState] = React.useState(false);
Nevertheless is there a way to load the font in the root page (at the start of the app/Where the instance of my app is launching) ? I want to avoid to load the font on each page.
Hi I want to add a function to record the value of input and want to use it in all components and input is in another component and I want to pass a function from App.js to that component but it does not work please help me out \
Please note that the function should be in the App.js passed to Component.js through a Navigator
//App.JS
import React,{useState, useEffect} from "react";
import {
Text,
Link,
HStack,
Center,
Heading,
Switch,
useColorMode,
NativeBaseProvider,
extendTheme,
VStack,
Box,
} from "native-base";
import NativeBaseIcon from "./components/NativeBaseIcon";
import { Platform } from "react-native";
import { createMaterialBottomTabNavigator } from '#react-navigation/material-bottom-tabs';
import HomeScreen from "./Comp/HomeScreen";
import { NavigationContainer } from "#react-navigation/native";
import Location from "./Comp/Location";
export default function App() {
const [api, setapi] = useState(process.env.REACT_APP_API_KEY)
const api_1 = () => {
setapi(process.env.REACT_APP_API_KEY_2)
}
const api_2 = () => {
setapi(process.env.REACT_APP_API_KEY_3)
}
const [value, setvalue] = useState("London")
const handlechange = (e) => {
setvalue(e.target.value)
console.log('value :>> ', value);
}
const Tab = createMaterialBottomTabNavigator();
return (
<NativeBaseProvider>
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen initialParams={{value:value, change:handlechange}} name="Location" component={Location} />
</Tab.Navigator>
</NavigationContainer>
</NativeBaseProvider>
);
}
//Component.js
import { Center, Input, Text } from 'native-base'
import React, {useState} from 'react'
import { ImageBackground } from 'react-native'
import BG from "./Img/BG.png"
export default function Location({ navigation, route }) {
// const [value, setvalue] = useState("London")
// const handlechange = (e) => {
// setvalue(e.target.value)
// console.log('value :>> ', value);
// }
const { change, value } = route.params;
return (
<ImageBackground source={BG}>
<Center minH={"100vh"} w={"90vw"} alignSelf={"center"} alignContent={"center"}>
<Input color={"white"} size="xl" onChange={change} variant="rounded" placeholder="Enter Location" />
<Text color={"white"}></Text>
</Center>
</ImageBackground>
)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
I have used createStackNavigator for Normal navigation and createMaterialTopTabNavigator for Tab navigation
Nested navigation is acting strangely. I have a StackNavigator in App.tsx that contains ProfileHome.tsx. The ProfileHome.tsx contains Tab navigations that have four screens. I wanted to navigate to the PastWorkDetail.tsx screen which is a part of the StackNavigator from PastWork.tsx (Tab)
Whenever I navigate from PastWork (Tab) to PastWorkDetail, the screen PastWorkDetail appears and automatically redirects to the previous screen, which is PastWork (Tab)
App.tsx
import { NavigationContainer } from "#react-navigation/native";
import { createStackNavigator } from "#react-navigation/stack";
import React, { useState } from "react";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { RootStackParamList } from "./navigation/types";
import LoginHome from "./screens/login/LoginHome";
import SplashScreen from "./screens/login/SplashScreen";
import SocialMediaSelectPage from "./screens/preference/SocialMediaSelectPage";
import PastWorkDetail from "./screens/profile/PastWorkDetail";
import ProfileHome from "./screens/profile/ProfileHome";
const RootStack = createStackNavigator<RootStackParamList>();
const App = () => {
return (
<SafeAreaProvider>
<NavigationContainer>
<RootStack.Navigator
initialRouteName="LoginHome">
<RootStack.Screen name="LoginHome" component={LoginHome} />
<RootStack.Screen name="SocialMediaSelectPage" component={SocialMediaSelectPage}/>
<RootStack.Screen name="ProfileHome" component={ProfileHome} />
<RootStack.Screen name="PastWorkDetail" component={PastWorkDetail} />
<RootStack.Screen name="Test" component={Test} />
</RootStack.Navigator>
</NavigationContainer>
</SafeAreaProvider>
);
};
export default App;
ProfileHome.tsx
import { createMaterialTopTabNavigator } from "#react-navigation/material-top-tabs";
import React from "react";
import { View } from "react-native";
import HeaderProfile from "../../components/header/HeaderProfile";
import PastWork from "./PastWork";
import Preferences from "./Preferences";
import Profile from "./Profile";
import TopTabBar from "./shared/TopTabBar";
import Stats from "./Stats";
interface TabItem {
name: string;
component: React.ComponentType<any>;
}
const tabs: TabItem[] = [
{ name: "Profile", component: Profile },
{ name: "Stats", component: Stats },
{ name: "Preferences", component: Preferences },
{ name: "Pastwork", component: PastWork },
];
const ProfileHome = () => {
const Tab = createMaterialTopTabNavigator();
return (
<View style={styles.container}>
<Tab.Navigator backBehavior="history" nitialRouteName="Profile" tabBar={(props) => <TopTabBar {...props} />}>
{
// Populating tabs
tabs.map((item: TabItem, index: number) => {
return (
<Tab.Screenkey={index} name={item.name} component={item.component}
options={{
tabBarLabel: item.name,
}}
/>
);
})
}
</Tab.Navigator>
</View>
);
};
export default ProfileHome;
PastWork.tsx
import { useNavigation } from "#react-navigation/native";
import React from "react";
import { Image, ImageSourcePropType, StyleSheet, View } from "react-native";
const images = [
require("../../assets/images/pastWork1.jpeg"),
require("../../assets/images/pastWork2.png")
];
const PastWork = () => {
const navigation = useNavigation();
const onPress = () => {
navigation.navigate("PastWorkDetail");
};
return (
<View style={styles.container}>
<View style={{ width: Utils.getWidth(45) }}>
{
images.map((item, index) => {
return (
<ImageView key={index} source={item} onPress={onPress} />
);
})
}
</View>
</View>
);
};
export default PastWork;
PastWorkDetail.tsx
import React from "react";
import { StyleSheet, View } from "react-native";
import Header from "../../components/header/Header";
const PastWorkDetail = () => {
return (
<View style={styles.container}>
<Header name="Pastwork Info" />
</View>
);
};
export default PastWorkDetail;
I am trying to wrap one of my navigators with User Context that I created. I have achieved this before in other projects but I am encountering an issue. I Tried following this solution but it doesn't seem to be the same issue I am encountering. I can't exactly tell what is wrong here.
App.js Code :
import React, { useContext, useEffect } from "react";
import { View, Text, AsyncStorage, Button } from "react-native";
import { createStackNavigator } from "#react-navigation/stack";
import HomeScreen from "./src/screens/HomeScreen";
import LoginScreen from "./src/screens/login";
import CalendarScreen from "./src/screens/Calendar";
import SignUpScreen from "./src/screens/signUp";
import { scale, vs } from "react-native-size-matters";
import { createDrawerNavigator } from "#react-navigation/drawer";
import { createMaterialBottomTabNavigator } from "#react-navigation/material-bottom-tabs";
import { Icon } from "react-native-elements";
import UserContext, { UserProvider } from "./src/screens/Context/UserContext";
import { NavigationContainer } from "#react-navigation/native";
const Tab = createMaterialBottomTabNavigator();
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
const signedIn = true; //this is for testing
const drawer_style = {
backgroundColor: "#202B35",
activeTintColor: "#000",
width: 200,
};
const drawer_item_style = {
activeTintColor: "orange",
inactiveTintColor: "#fff",
itemStyle: { marginVertical: vs(10) },
};
const non_user_stack = () => {
<Stack.Navigator>
<Stack.Screen
name="Sign in - Cal "
component={LoginScreen}
options={({ navigation }) => ({
headerShown: true,
headerTintColor: "orange",
headerStyle: {
backgroundColor: "#202B35",
},
})}
/>
<Stack.Screen
name="Sign up - Cal "
component={SignUpScreen}
options={({ navigation }) => ({
headerShown: true,
headerTintColor: "orange",
headerStyle: {
backgroundColor: "#202B35",
},
})}
/>
</Stack.Navigator>;
};
const UserMenu = () => {
return (
<NavigationContainer>
<Drawer.Navigator
initialRouteName="Home"
drawerStyle={drawer_style}
drawerContentOptions={drawer_item_style}
drawerType="front"
>
<Drawer.Screen name="Home" component={MyStack} />
</Drawer.Navigator>
</NavigationContainer>
);
};
const MyStack = () => {
return(
<Stack.Navigator>
<Stack.Screen
name="Cal"
component={BottomNav}
options={({ navigation }) => ({
headerShown: true,
headerTintColor: "orange",
headerStyle: {
backgroundColor: "#202B35",
},
headerLeft: (props) => (
<Icon
size={25}
name={"ios-menu"}
iconStyle={{
fontSize: scale(30),
color: "orange",
margin: 5,
marginLeft: 10,
}}
type="ionicon"
color="orange"
onPress={() => {
navigation.toggleDrawer();
}}
/>
),
})}
/>
</Stack.Navigator>
)
};
export default App = () => {
const { isSignedIn, check_and_set_signin_status } = useContext(UserContext); //<-- causes crash
return (
isSignedIn === "false" ? (
<UserProvider>
<UserMenu />
</UserProvider>
) : (
<non_user_stack></non_user_stack>
);
);
};
UserContext.js :
import React, { useState, useEffect } from "react";
import * as Permissions from "expo-permissions";
import axios from "axios";
import { AsyncStorage } from "react-native";
//import registerForPushNotificationsAsync from "../Hooks/registerForPushNotifications";
import Constants from "expo-constants";
const UserContext = React.createContext();
const IS_SIGNEDIN = "is_signed_in";
export const UserProvider = ({ children }) => {
const [isSignedIn, setSignIn] = useState(null);
const [didAuthenticate, setAuthenticated] = useState(null);
//Check if this user already signed in before and didnt log out since their last session
//used for conditional rendering
const check_and_set_signin_status = async () => {
const signed_in = await AsyncStorage.getItem(IS_SIGNEDIN);
if (signed_in == null || signed_in == "false") {
await AsyncStorage.setItem(IS_SIGNEDIN, "false");
setSignIn("false");
} else {
setSignIn("true");
}
};
return (
<UserContext.Provider
value={{
isSignedIn, // well use this for conditional rendering
check_and_set_signin_status,
}}
>
{children}
</UserContext.Provider>
);
};
The Error :
there is some mistake in your code
you are not exporting UserContext but you are importing UserContext
in App.js file
you are trying to use useContext and provider in same file but you
have to useContext inside of Provider child component
you are non_user_stack with first letter capital but you have to
make first letter capital
UserContext.js : you have to export UserContext in this file
import React, { useState, useEffect } from "react";
import { Text } from 'react-native'
import * as Permissions from "expo-permissions";
import axios from "axios";
import { AsyncStorage } from "react-native";
//import registerForPushNotificationsAsync from "../Hooks/registerForPushNotifications";
import Constants from "expo-constants";
const UserContext = React.createContext();
export default UserContext;
const IS_SIGNEDIN = "is_signed_in";
export const UserProvider = ({ children }) => {
const [isSignedIn, setSignIn] = useState(null);
const [didAuthenticate, setAuthenticated] = useState(null);
const check_and_set_signin_status = async () => {
const signed_in = await AsyncStorage.getItem(IS_SIGNEDIN);
if (signed_in == null || signed_in == "false") {
await AsyncStorage.setItem(IS_SIGNEDIN, "false");
setSignIn("false");
} else {
setSignIn("true");
}
};
return (
<UserContext.Provider
value={{
isSignedIn, // well use this for conditional rendering
check_and_set_signin_status,
}}
>
{children}
</UserContext.Provider>
);
};
App.js Code :
const App = () => {
const { isSignedIn, check_and_set_signin_status } = useContext(UserContext); //<-- causes crash
console.log( isSignedIn, check_and_set_signin_status ,"useContext")
return isSignedIn === "false" ? (
<UserMenu />
) : (
<Non_user_stack></Non_user_stack>
);
};
const jsx = () => (
<UserProvider>
<App />
</UserProvider>
);
export default jsx;
In my case I imported badly
import ThemeContext from '../contexts/theme-context';
Instead
import { ThemeContext } from '../contexts/theme-context';
You should always check what you're exporting from your context folder
In my case I import { LocalContext } from ".././services/location/location.context";
instead of import { LocationContext } from ".././services/location/location.context";
Mine was:
I mistakenly imported the file (languageContext.js) instead of its function {LanguageContext} so now it goes like this where I called my context.
import {LanguageContext} from "../../Context/languageContext";
const { language } = useContext(languageContext);
i18n.locale = language;