Stack Navigator issue while loading in React Native - react-native

About
Version of Stack Navigation - #react-navigation/stack": "^5.9.3"
I am trying to show the home screen on load.
Error Details
Code in App.js
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import NavigationContainer from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import Home from './screens/home';
export default function App() {
const Stack = createStackNavigator();
return (
<View style={styles.container}>
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={Home} />
</Stack.Navigator>
</NavigationContainer>
</View>
);
}
Code in Home screen
import React, { useState } from 'react';
import { Text, View } from 'react-native';
export default function Home() {
return (
<View>
<Text>Home Screen</Text>
</View>
)
}
Versions
"#react-navigation/drawer": "^5.9.3",
"#react-navigation/native": "^5.7.6",
"#react-navigation/stack": "^5.9.3",
"expo": "~39.0.2",
"expo-status-bar": "~1.0.2",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-39.0.3.tar.gz",
"react-native-web": "~0.13.12"

You need to wrap the <MyStack /> with the NavigationContainer tags
import {
NavigationContainer,
} from '#react-navigation/native';
<NavigationContainer>
<MyStack />
</NavigationContainer>
As per the package.json you are using two versions of navigation so update all to navigation 5 something like below
"#react-navigation/native": "^5.4.0",

Possible causes:
Stack navigator should be <NavigationContainer>'s immediate child.
If your React version does not support hooks use older version of react navigation.

Related

React-Navigation requireNativeComponent: "RNSScreenStackHeaderConfig" was not found in the UIManager

=> MacOS Monterey Version 12.6.2
=> Xcode Version 14.2
Bellow is my package version:-
"#react-native-async-storage/async-storage": "^1.17.11",
"#react-navigation/native": "^6.1.2",
"#react-navigation/native-stack": "^6.9.8",
"react": "18.1.0",
"react-native": "0.70.6",
"react-native-safe-area-context": "^4.4.1",
"react-native-screens": "^3.18.2"
Hear is my Navigation File code:-
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import * as React from 'react';
import ScreenManager from './Screens/ScreenManager';
const Stack = createNativeStackNavigator();
export default MyStack = () => {
return (
<NavigationContainer>
<Stack.Navigator
screenOptions={{
headerShown: false
}}
>
<Stack.Screen
name="Home"
component={ScreenManager}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
Showing this error in Simulator
i am install react navigation dependency from follow bellow url =>
https://reactnative.dev/docs/navigation
and also try pod install command. but still getting fail to run my application.

Unable to resolve module 'module://#babel/runtime/helpers/interopRequireDefault.js' - EXPO SNACK

I am getting the error 'Unable to resolve module 'module://#babel/runtime/helpers/interopRequireDefault.js' when I try to run the following code in Expo Snack
Below is my code for HomeScreen.js
import { StyleSheet, Text, View, FlatList } from "react-native";
import axios from "axios";
import { useState, useEffect } from "react";
export default function HomeScreen() {
const [cycles, setCycles] = useState([]);
useEffect(() => {
const fetchData = async () => {
try {
const { data: response } = await axios.get(
"URL"
);
setCycles(response);
} catch (error) {
console.error(error.message);
}
};
fetchData();
}, []);
return (
<View style={styles.container}>
{/* <Text> {JSON.stringify(cycles[0])} </Text> */}
<FlatList
data={cycles}
renderItem={(
{ item } //this part will iterate over every item in the array and return a listItem
) => (
<Text>
{new Date(item.startDate).toLocaleDateString("us-EN")} -{" "}
{new Date(item.endDate).toLocaleDateString("us-EN")}{" "}
</Text>
)}
/>
<Text>Hello</Text>
</View>
);
}
And below is App.js
import { StyleSheet, Text, View, FlatList } from "react-native";
import axios from "axios";
import { useState, useEffect } from "react";
import HomeScreen from "./screens/HomeScreen";
import { NavigationContainer } from "#react-navigation/native";
import { createNativeStackNavigator } from "#react-navigation/stack";
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{ title: "Welcome" }}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
Package.json file
{
"dependencies": {
"react-native-paper": "4.9.2",
"#expo/vector-icons": "^13.0.0",
"expo-constants": "~13.2.4",
"axios": "*",
"#react-navigation/stack": "*",
"#react-navigation/native": "6.0.0",
"react-native-gesture-handler": "~2.5.0",
"react-native-safe-area-context": "4.3.1",
"react-native-screens": "~3.15.0"
}
}
From one of the questions I found Unable to resolve module `#babel/runtime/helpers/interopRequireDefault`, I tried adding babel and it has not worked.(ie it throws another error as below)
Thanks for any help!
Downgrade axios to 0.27.2 to align with Expo built-in #babel/runtime version.

React Native Stack.Navigator does't work on Android

I have an issue with Stack.Navigator on Android. It just doesn't work by some strange reason.
There is simple code:
import React from 'react';
import {createNativeStackNavigator} from '#react-navigation/native-stack';
import {ROUTES} from '../../constants/routes';
import {AuthScreen, LoginScreen, RegisterScreen} from './screens';
export const Unauthorized = () => {
const Stack = createNativeStackNavigator();
return (
<Stack.Navigator
initialRouteName={ROUTES.ROUTE_AUTH}
screenOptions={{headerShown: false}}>
<Stack.Screen name={ROUTES.ROUTE_AUTH} component={AuthScreen} />
<Stack.Screen name={ROUTES.ROUTE_LOGIN} component={LoginScreen} />
<Stack.Screen name={ROUTES.ROUTE_REGISTER} component={RegisterScreen} />
</Stack.Navigator>
);
};
Looks simple. AuthScreen component also is simple:
import React from 'react';
import {SafeAreaView, StyleSheet, Text} from 'react-native';
export const AuthScreen = () => {
return (
<SafeAreaView>
<Text style={styles.text}>Auth screen</Text>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
text: {
color: 'red',
},
});
There is a App.tsx:
import 'react-native-gesture-handler';
import React from 'react';
import {SafeAreaView, StatusBar, useColorScheme} from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen';
import {ApolloProvider} from '#apollo/client';
import client from './src/apollo';
import {SplashScreen} from './src/screens';
const App = () => {
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
return (
<ApolloProvider client={client}>
<SafeAreaView style={backgroundStyle}>
<SplashScreen />
<StatusBar backgroundColor="white" barStyle="dark-content" />
</SafeAreaView>
</ApolloProvider>
);
};
export default App;
But screen is empty when app is Running.
But if I remove Stack.Navigator at all then the content gets visible
Any notes with dependencies or MainActivity I have done.
There is deps:
"#apollo/client": "^3.7.1",
"#react-navigation/native": "^6.0.6",
"#react-navigation/native-stack": "^6.2.5",
"#react-navigation/stack": "^6.0.11",
"graphql": "^16.6.0",
"react": "18.1.0",
"react-hook-form": "^7.39.1",
"react-native": "0.70.4",
"react-native-bootsplash": "^4.3.3",
"react-native-gesture-handler": "^2.8.0",
"react-native-mmkv-storage": "^0.8.0",
"react-native-safe-area-context": "^4.4.1",
"react-native-screens": "^3.10.0"
I spent 4 hours and I have no more ideas how to get Stask.Navigator to show content....
I tried to use
import {createStackNavigator} from '#react-navigation/stack'
const Stack = createStackNavigator()
instead of createNativeStackNavigator
I tried to change versions. Nothing helped.
give flex:1 style to SafeAreaView in App.js

Error: Can't find variable Stack, React Navigation

I am trying to develop a todolist app using react native, for which I coded the home screen. I had following some docs, but it gives me this error. Please help.
I have executed the commands in the following order.
expo install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context #react-native-community/masked-view
npm install #react-navigation/native
npm install #react-navigation/stack
This is my code.
import React from 'react';
import { StyleSheet, Text, View ,Button} from 'react-native';
import 'react-native-gesture-handler';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator} from '#react-navigation/stack';
class HomeScreen extends React.Component {
constructor(props){
super(props);
this.state={}
}
render(){
return(
<View>
<View style={{margin:50}}>
<Button title="New Task"></Button>
</View>
</View>
)
}
}
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
You are not declaring Stack in your program include this line in your program:
const Stack = createStackNavigator();
Add this line in your App function like this:
export default function App() {
const Stack = createStackNavigator();
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
Hope this helps!
Add this line to the top of your code
import { createStackNavigator } from "#react-navigation/stack";
In case someone looking for without using class...
import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
function HomeScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
}
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
Try on expo, click here
write this instead if you are using react native:
import { createNativeStackNavigator } from '#react-navigation/native-stack';
&&
const Stack = createNativeStackNavigator();

React native custom Drawer navigation

I have created Custom DrawerNavigator. Everything is in place. But, I am not able to navigate between screens. I do not know how to navigate so i follow instructions at https://github.com/kakulgupta/CustomDrawer/tree/master/app. But this did nt help
import React,{Component} from 'react'
import {Text,View,StyleSheet,Image,NavigationActions} from 'react-native'
import {Icon,Container,Header,Content,Left} from 'native-base'
export default class Head extends Component{
navigateToScreen = (route) =>()=>{
const navAction = NavigationActions.navigate({
routeName:route
})
this.props.navigation.dispatch(navAction)
}
render(){
return (
<View>
<View style={styles.image}>
<Image
style={{height:200,
width:90,
marginLeft:'30%',
marginRight:'30%',
marginTop:'15%',
alignContent:'center',
alignItems:'center'
}}
source={require('./../images/logo.png')}
/>
</View>
<View style={styles.menu}>
<View style={styles.box} >
<Icon name="person" />
<Text style={styles.boxText} onPress={this.navigateToScreen("Login")}>Login</Text>
</View>
</View>
</View>
)
}
}
And my app.js is
import React, {Component} from 'react';
import {DrawerNavigator} from 'react-navigation'
import Login from './src/Login/login.js'
export default class App extends Component{
render(){
return(
<Links />
)
}
}
const Links = DrawerNavigator({
Login:{screen:Login},
},{contentComponent:props => <Slider {...props}/>
})
package.json
"dependencies": {
"native-base": "^2.8.0",
"react": "^16.4.1",
"react-native": "^0.55.4",
"react-navigation": "^2.12.1"
},
And the error is
undefined is not an object (evaluating
'_reactNative.NavigationActions.navigate')
Any help please.
You've imported NavigationActions from wrong place, you need to import it from react-navigation
import {NavigationActions} from 'react-navigation'