React Native <NavigationContainer> not styling - react-native

Building a Signal clone and trying to have my to be styled from my StyleSheet. Here is the code
import "react-native-gesture-handler";
import { StatusBar } from "expo-status-bar";
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import { NavigationContainer } from "#react-navigation/native";
import LoginScreen from "./screens/LoginScreen";
import { createStackNavigator } from "#react-navigation/stack";
import RegisterScreen from "./screens/RegisterScreen";
const Stack = createStackNavigator();
const globalScreenOptions = {
headerStyle: { backgroundColor: "#2C6BED" },
headerTitleStyle: { color: "white" },
headerTintColor: "white",
};
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator screenOptions={globalScreenOptions}>
<Stack.Screen name="Login" component={LoginScreen} />
<Stack.Screen name="Register" component={RegisterScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
I have tried to put everything in a tag to get it to be affected by the StyleSheet, but that won't work either. I'm unable to find any other similar posts to this question

To use the styles created by StyleSheet.create, you need to actually use them in an element. For example (from the stylesheet docs):
import React from "react";
import { StyleSheet, Text, View } from "react-native";
const App = () => (
<View style={styles.container}> {/* <-- note style attribute */}
<Text style={styles.title}>React Native</Text>
</View>
);
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 24,
backgroundColor: "#eaeaea"
},
title: {
marginTop: 16,
paddingVertical: 8,
textAlign: "center",
}
});
export default App;

Related

The action 'NAVIGATE' with payload {"name":"Man"} was not handled by any navigator. Do you have a screen named 'Man'? when i dont set a stack.screen

your texti'm tryng to navigate with the props navigation as a nested navigation and i'm getting this error
ERROR The action 'NAVIGATE' with payload {"name":"Man"} was not handled by any navigator.
Do you have a screen named 'Man'?
If you're trying to navigate to a screen in a nested navigator, see https://reactnavigation.org/docs/nesting-navigators#navigating-to-a-screen-in-a-nested-navigator.
This is a development-only warning and won't be shown in production.
i suspact that is becose i didnt set the screen but i dont wont to set the screen becose i wont it to render only when the user is clicking on the button
i'm tryng to navigate the component man
this is my code
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
export default function Man() {
return (
<View style={styles.container}>
<Text>Man</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
import { NavigationContainer } from "#react-navigation/native";
import { createBottomTabNavigator } from "#react-navigation/bottom-tabs"
import { useNavigation } from '#react-navigation/native';
import Home from "./screens/Home";
import Exhibition from "./screens/Exhibition";
import Cart from "./screens/Cart";
export default function App() {
const Tab = createBottomTabNavigator();
// const navigation = useNavigation();
return (
<NavigationContainer>
<Tab.Navigator >
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Exhibition" component={Exhibition} />
<Tab.Screen name="Cart" component={Cart} />
</Tab.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
// marginLeft: 50
},
});
import { StatusBar } from "expo-status-bar";
import {createStackNavigator} from '#react-navigation/stack';
import {
FlatList,
Image,
StyleSheet,
Text,
TouchableOpacity,
View,
} from "react-native";
import { createNativeStackNavigator } from "#react-navigation/native-stack";
import { useState } from "react";
import { useNavigation, StackActions, useNavigationContainerRef } from "#react-navigation/native";
import Man from "./Man";
import Woman from './Woman'
export default function Home({navigation}: any) {
const navigationRef = useNavigationContainerRef()
const collections: { name: string; img: any; compo: any; key: number }[]= [
{
name: "man collection",
img: require("../assets/manModel.jpeg"),
compo: "Man",
key: 1,
},
{
name: "woman collection",
img: require("../assets/womanModel.jpg"),
compo: "Woman",
key: 2,
},
]
const Stack = createNativeStackNavigator();
// const navigation = useNavigation();
return (
<View style={styles.container}>
<FlatList
numColumns={2}
data={collections}
renderItem={({ item }) => (
<>
<TouchableOpacity
onPress={() => {
navigation.navigate('Man');
}}
>
<Image style={styles.image} source={item.img} />
<Text style={styles.title}>{item.name}</Text>
</TouchableOpacity>
</>
)}
/>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
image: {
width: 170,
height: 180,
margin: 15,
},
title: {
position: "absolute",
left: "15%",
right: "15%",
width: "70%",
top: 90,
fontSize: 17,
color: "white",
backgroundColor: "purple",
fontWeight: "bold",
textAlign: "center",
},
});

Why am I getting an error like this, "undefined is not an object route.key"?

I am trying to use the react-navigation library to create navigation between two components.
This is my code:
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* #format
* #flow strict-local
*/
import React from 'react';
import type {Node} from 'react';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
} from 'react-native';
import {
Colors,
DebugInstructions,
Header,
LearnMoreLinks,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
const Section = ({children, title}): Node => {
const isDarkMode = useColorScheme() === 'dark';
return (
<View style={styles.sectionContainer}>
<Text
style={[
styles.sectionTitle,
{
color: isDarkMode ? Colors.white : Colors.black,
},
]}>
{title}
</Text>
<Text
style={[
styles.sectionDescription,
{
color: isDarkMode ? Colors.light : Colors.dark,
},
]}>
{children}
</Text>
</View>
);
};
const App: () => Node = () => {
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
function HomeScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
}
const Stack = createNativeStackNavigator();
return (
<>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
</>
);
};
const styles = StyleSheet.create({
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
},
highlight: {
fontWeight: '700',
},
});
export default App;
I am getting an error like this:
undefined is not an object route.key
I tried installing the necessary libraries through the react-navigation website and the version is 6x. Please let me know where am I doing the error. It has been like 2-3 days and I am finding no luck with this library.
Try to implement other dependency library perfectly like below
npm install #react-navigation/native
npm install react-native-screens
npm install react-native-safe-area-context
npm install #react-navigation/native-stac
After that you can check the code below how it’s done.
import React from 'react';
import type { Node } from 'react';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native- stack';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
} from 'react-native';
const Stack = createNativeStackNavigator();
function HomeScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
}
const App: () => Node = () => {
return (
<>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
</>
);
};
const styles = StyleSheet.create({
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
},
highlight: {
fontWeight: '700',
},
});
export default App;

Issue creating bottom-tab

I have created a login screen for my expo react-native app. However, once the the user has logged in, at the home screen, I would like to show 3 icons at the bottom tab.But i am not sure how to create the bottom tabs. Essentially, there will be 3 tabs that need to be created (e.g. calendar, activity and settings).
App.js
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import LoginScreen from './screens/Dashboard/LoginScreen';
import HomeScreen from './screens/Dashboard/HomeScreen';
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen options={{ headerShown: false }} name="Login" component={LoginScreen} />
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
HomeScreen.js
import { useNavigation } from '#react-navigation/core'
import React from 'react'
import { StyleSheet, Text, TouchableOpacity, View, Image } from 'react-native'
import { auth } from '../../firebase'
const HomeScreen = () => {
const navigation = useNavigation()
const handleSignOut = () => {
auth
.signOut()
.then(() => {
navigation.replace("Login")
})
.catch(error => alert(error.message))
}
return (
<View style={styles.container}>
<Text>Email: {auth.currentUser?.email}</Text>
<TouchableOpacity
onPress={handleSignOut}
style={styles.button}
>
<Text style={styles.buttonText}>Sign out</Text>
</TouchableOpacity>
</View>
)
}
export default HomeScreen
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
button: {
backgroundColor: '#0782F9',
width: '60%',
padding: 15,
borderRadius: 10,
alignItems: 'center',
marginTop: 40,
},
buttonText: {
color: 'white',
fontWeight: '700',
fontSize: 16,
},
image: {
width: 40,
height: 40,
},
})
Would appreciate your help. Thanks.
Create a component like shown in react navigation docs https://reactnavigation.org/docs/material-bottom-tab-navigator/#example and then replace the "HomeScreen" component with the newly create Tabs navigator component.
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen options={{ headerShown: false }} name="Login" component={LoginScreen} />
<Stack.Screen name="Home" component={MyTabs} />
</Stack.Navigator>
</NavigationContainer>
);
}
[1]: https://reactnavigation.org/docs/material-bottom-tab-navigator

Couldn't find a 'component' or 'children' prop for the screen 'Home'

I'm trying to use react-navigate v5 to setup a stacknavigator for some screens. Currently I'm getting this error while trying to run the app:enter image description here
My App.js:
import React from 'react';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import SignIn from './App/Screens';
const AuthStack = createStackNavigator();
export default () => (
<NavigationContainer>
<AuthStack.Navigator>
<AuthStack.Screen name='SignIn' component = {SignIn} />
</AuthStack.Navigator>
</NavigationContainer>
);
my Screens.js:
import React from 'react';
import {View, Text, StyleSheet, Button, TouchableOpacity } from 'react-native';
const styles = StyleSheet.create({
container: {
padding: 20,
justifyContent: 'center',
},
input: {
height: 40,
backgroundColor: "rgba(255, 255, 255, 0.2)",
marginBottom: 10,
color: '#FFF',
paddingHorizontal: 10
},
buttonContainer: {
backgroundColor: '#2980b9',
paddingVertical: 10,
marginBottom: 10
},
buttonText: {
textAlign: 'center',
color: '#FFF',
fontWeight: '700'
}
});
const ScreenContainer = ({ children }) => (
<View style={styles.container}>{children}</View>
);
export const SignIn = ({ navigation }) => {
return (
<ScreenContainer>
<Text>Sign In Screen</Text>
<Button title="Sign In" onPress={() => alert("todo") } />
<Button
title="Create Account"
onPress={() => alert("todo")}
/>
</ScreenContainer>
);
};
Not sure what's going on, would appreciate some help. Thanks!
You should change your import from, as you are using a named export
import SignIn from './App/Screens';
to
import {SignIn} from './App/Screens';

Custom React Drawer Navigation

i'm currently trying to implement custom component to show the icon i've.
i got no error when rendering it , but 2 of list of screen didn't shows up. am i need to do something with styling or there's a problem with my code
if anyone could inspect my code that would be awesome
this is my error
here are my code
App.js
import React from 'react';
import {
StyleSheet,
View,
SafeAreaView,
ScrollView,
Dimensions,
Image,
Text
} from 'react-native';
import {
createDrawerNavigator,
DrawerItems
} from 'react-navigation';
import HomeScreen from './screen/HomeScreen';
import SettingsScreen from './screen/SettingsScreen';
export default class App extends React.Component {
render () {
return (
<AppDrawerNavigator />
);
}
}
const CustomDrawerComponent = ( props ) => (
<SafeAreaView style={{flex: 1}}>
<ScrollView>
<View style={{height:150,backgroundColor:'white',alignItems: 'center', justifyContent: 'center' }}>
<Image
source={require('./img/cs.png')}
style={{height:120, width:120, borderRadius: 20}}
/>
</View>
</ScrollView>
</SafeAreaView>
)
const AppDrawerNavigator = createDrawerNavigator ({
Home: HomeScreen,
Settings: SettingsScreen
},{
contentComponent: CustomDrawerComponent
})
const styles = StyleSheet.create({
container: {
flex: 0,
color: '#fff',
alignItems: 'center',
justifyContent: 'center'
}
});
Homescreen.js
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';
class HomeScreen extends Component {
render () {
return (
<View style={styles.container}>
<Text>Home</Text>
</View>
);
}
}
export default HomeScreen ;
const styles = StyleSheet.create({
container: {
flex: 1,
color: '#fff',
alignItems: 'center',
justifyContent: 'center'
}
});
According to the official doc, your Drawer Navigator has to be defined like:
const AppDrawerNavigator = createDrawerNavigator({
Home: {
screen: HomeScreen,
},
Settings: {
screen: SettingsScreen,
}
});
You're not assigning an object with a prop screen to each custom screen.
For more details take a look at doc.
as I understand, you need to add DrawerItems to your drawer.
import { DrawerItems, SafeAreaView } from 'react-navigation';
const CustomDrawerComponent = ( props ) => (
<SafeAreaView style={{flex: 1}}>
<ScrollView>
<View style={{height:150,backgroundColor:'white',alignItems: 'center', justifyContent: 'center' }}>
<Image
source={require('./img/cs.png')}
style={{height:120, width:120, borderRadius: 20}}
/>
</View>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
)