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
Related
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.
I'm started with React Native and try to use the navigation between screns. To do so I creat a component Navigation but when I want to use it this error show up :
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined
It appear to be the component AppNavigator that is the issue
I tried :
checked all my exports and import and for me their is no problems
change my const component in a function component but nothing do the trick.
Can you help me ?
App.js :
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import AppNavigator from './components/navigation';
function App() {
return (
<View>
<AppNavigator />
</View>
);
}
/*const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
justifyContent: 'center',
},
});*/
export default App;
Navigation.js
import React from 'react';
import { createStackNavigator } from '#react-navigation/stack';
import { NavigatorContainer } from '#react-navigation/native';
import Localisation from './localisation';
import Home from './home';
const { Navigator, Screen } = createStackNavigator()
const AppNavigator = () => (
<NavigatorContainer>
<Navigator initialRouteName="Home">
<Screen options={{headerShown: false}} name="Home" component={Home} />
</Navigator>
</NavigatorContainer>
)
export default AppNavigator;
There is no component named NavigatorContainer in react-navigation/native. The component is called NavigationContainer.
Hence, the following should fix the issue.
import React from 'react';
import { createStackNavigator } from '#react-navigation/stack';
import { NavigationContainer } from '#react-navigation/native';
import Localisation from './localisation';
import Home from './home';
const { Navigator, Screen } = createStackNavigator()
const AppNavigator = () => (
<NavigationContainer>
<Navigator initialRouteName="Home">
<Screen options={{headerShown: false}} name="Home" component={Home} />
</Navigator>
</NavigationContainer>
)
export default AppNavigator;
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.
import React from 'react';
import {TextInput, StyleSheet, Text, View } from 'react-native';
import {Button} from 'react-native-elements';
import { Navigation } from 'react-native-navigation';
import Signup from "./Signup";
export default function App() {
return (
<View style={styles.container}>
<Text style={{marginTop: 0}}>Junior Facebook</Text>
<TextInput placeholder="Email" style={{width: 80, height: 30}}/>
<TextInput placeholder="Password" style={{width:80, height: 30}}/>
<Button title="Log In">Log In</Button>
<Button title="Sign Up">Sign Up</Button>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
I am trying to make a new screen called "Signup". How can I?
Thank you
I heard I can use react navigation so I googled it but it does not work either.
Please help me out here,
You need to install Stack Navigator module to do this.
Create Stack navigator with screen like below code. (here i have used signin Page and signup Page, Please change to your pages and also import).
import this stack navigator into app.js file and wraps it with navigation container;
Link to official docs
Stack Navigator
import React from "react";
import { createStackNavigator } from "#react-navigation/stack";
import SignIn from "../screens/auth/SignIn";
import SignUp from "../screens/auth/SignUp";
const AuthStack = () => {
const AuthStack = createStackNavigator();
return (
<AuthStack.Navigator>
<AuthStack.Screen
name="Sign In"
component={SignIn}
options={{
headerTitle: "",
headerTransparent: true
}}
/>
<AuthStack.Screen
name="Sign Up"
component={SignUp}
options={{
headerTitle: "",
headerTransparent: true
}}
/>
</AuthStack.Navigator>
);
};
export default AuthStack;
App.js
import other stuff;
import { NavigationContainer } from "#react-navigation/native";
import AuthStackScreen from "./routes/AuthStack";
export default App = () => {
return (
<SafeAreaView>
<NavigationContainer>
<AuthStackScreen />
</NavigationContainer>
</SafeAreaView>
);
SignIn.js
const SignIn = ({navigation}) => {
return (
<button onClick={navigation.navigate("Sign Up")}>Click to Navigate</button>
);
}
Package.json
"#react-native-community/masked-view": "0.1.5",
"#react-navigation/native": "^5.0.5",
"#react-navigation/stack": "^5.0.5",
"react-native-safe-area-context": "0.6.0",
"react-native-screens": "2.0.0-alpha.12",
"react-native-web": "^0.11.7",
"react-native-webview": "7.4.3"
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'