React Native : my component isn't detected as a component - react-native

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;

Related

unable to change the color of header stlye in react native

I'm starting to learn react native and I'm trying to change the header color but when I run the app only the text color changed to white. And the terminal shows some warning. Are both of them related? Here are some screenshots of it.
import 'react-native-gesture-handler';
import { setStatusBarBackgroundColor, StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet} from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import LoginScreen from './screens/LoginScreen';
const globalScreenOptions ={
headerStyle: { BackgroundColor:"dodgerblue" },
headerTitleStyle: {color: "white"},
headerTinColor:"white",
};
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator screenOptions={globalScreenOptions} >
<Stack.Screen name='Login' component={LoginScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
enter image description here
enter image description here
Try This its working you must provide style on Stack.Screen
<Stack.Screen options={{headerStyle:{backgroundColor:'dodgerblue'}}}name="Settings" component={SettingsScreen} />

How to open drawer in react native using navigation.openDrawer()

I wanted to create a burger icon to open drawer but has been unable to do so. I have included the icon button on the right of my header. Whenever I click it to open the drawer, it shows the error of undefined function. I have mentioned the error below.
TypeError: navigation.openDrawer is not a function. (In 'navigation.openDrawer()', 'navigation.openDrawer' is undefined)
This is App.js, in this I have mentioned the code for drawer using createDrawerNavigator()
App.js
import React from 'react';
import { NavigationContainer } from '#react-navigation/native';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import PassengerScreen from './Passenger';
import Lovedones from './Lovedones';
import NewTab from './FirstScreen';
import { createDrawerNavigator } from '#react-navigation/drawer';
const Drawer = createDrawerNavigator();
export default App=() => (
<NavigationContainer>
<Drawer.Navigator initialRouteName="Passenger" drawerPosition="right" >
<Drawer.Screen name="Passenger" component={PassengerScreen} options= {{ title:'Passenger'}} />
<Drawer.Screen name="Lovedones" component={Lovedones} options= {{ title:'loved ones!!'}} />
<Drawer.Screen name="New Screen" component={NewTab} />
</Drawer.Navigator>
</NavigationContainer>
);
This is AccountScreen.js, in this code, I have created the icon button to open the drawer using navigation.openDrawer()
AccountScreen.js
import * as React from 'react';
import { View, Text, StyleSheet, Button } from 'react-native';
import { createStackNavigator } from '#react-navigation/stack';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import Ionicons from 'react-native-vector-icons/Ionicons';
import { createDrawerNavigator } from '#react-navigation/drawer';
const Stack= createStackNavigator();
Account = () => {
return <View style={styles.container}>
<Text>Hello!!</Text>
</View>
};
export default AccountStack =(navigation)=>(
<Stack.Navigator >
<Stack.Screen name="Account" component= {Account} options={{headerRight: () => (<Ionicons.Button name="reorder-three" color={"#FF0000"} size={40} backgroundColor= {"none"} onPress={() => navigation.openDrawer()}/> ) }} />
</Stack.Navigator>
);
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
Kindly help me in identifying the error.
The problem is in this line
export default AccountStack =(navigation)=>.
Here your navigation variable points to the props instead of props.navigation.
To fix this you should destructure your navigation object from the props and change this line to
export default AccountStack =({navigation})=>

React Native Navigation - Invalid hook call

App.js:
import React, { useState } from "react";
import { Text } from "react-native";
import { NavigationContainer } from "#react-navigation/native";
import Stack from "./navigation/Stack";
export default function App() {
const [isReady, setIsReady] = useState(true);
const onFinish = () => setIsReady(true);
return isReady ? (
<NavigationContainer>
<Stack />
</NavigationContainer>
) : (
<Text>Loading...</Text>
);
}
Trying to navigate to a screen through a custom component:
import React from "react";
import { createStackNavigator } from "#react-navigation/stack";
import Home from "../screens/Home";
import Detail from "../screens/Detail";
const Stack = createStackNavigator();
export default () => (
<Stack.Navigator>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Detail" component={Detail} />
</Stack.Navigator>
);
Im getting an error:
Invariant Violation: Invalid hook call. Hooks can only be called
inside of the body of a function component.
Can someone have a look on it?

React Navigation Error (undefined is not an object (evaluating 'Component.router.getStateForAction'))

I am Trying to create a simple navigation in React Native. but I keep getting this error that I think is linked to the react Native navigation library.
Here is my App.js code :
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
import HomeScreen from "./src/screens/HomeScreen";
import { createMaterialBottomTabNavigator } from "#react-navigation/material-bottom-tabs";
const Tab = createMaterialBottomTabNavigator();
function MyTabs() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
</Tab.Navigator>
);
}
export default createAppContainer(MyTabs);
This is the error its generating :
You are mixing two versions of Navigators here, the createAppContainer is used with Navigation version and the createMaterialBottomTabNavigator is used with navigation version 5. If you want to use createMaterialBottomTabNavigator the code should look like below.
import * as React from 'react';
import { Text, View } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createMaterialBottomTabNavigator } from '#react-navigation/material-bottom-tabs';
function HomeScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
}
const Tab = createMaterialBottomTabNavigator();
export default function MyTabs() {
return (
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
</Tab.Navigator>
</NavigationContainer>
);
}

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();