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"
Related
i started to learn react native and i am getting error because i cant use openDrawer and i don't know where is the error.
i tried to follow the instructions on the react navigation web page.
below is my code
MainHeader
import React from 'react';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import Icon from '../../utils/Icon';
import {View} from 'react-native';
import {spacing} from '../../constants/theme';
const MainHeader = ({navigation}) => {
const insets = useSafeAreaInsets();
return (
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignContent: 'center',
paddingHorizontal: spacing.l,
marginTop: insets.top,
}}>
<Icon icon="Hamburger" onPress={() => navigation.openDrawer()} />
<Icon style={{width: 25, height: 25}} icon="Cart" onPress={() => {}} />
</View>
);
};
export default MainHeader;
MainNavigator
import React from 'react';
import {createStackNavigator} from '#react-navigation/stack';
import {NavigationContainer} from '#react-navigation/native';
import {StatusBar} from 'react-native';
import TabNavigator from './TabNavigator';
const Stack = createStackNavigator();
const MainNavigator = () => {
return (
<NavigationContainer>
<StatusBar hidden />
<Stack.Navigator>
<Stack.Screen
name="Root"
component={TabNavigator}
options={{
headerShown: false,
useNativeDriver: true,
gestureEnabled: false,
}}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
export default MainNavigator;
TabNavigator
import React from 'react';
import {createBottomTabNavigator} from '#react-navigation/bottom-tabs';
import {Animated} from 'react-native';
import Icon from '../utils/Icon';
import {colors, sizes} from '../constants/theme';
import PromoScreen from '../screens/PromoScreen';
import HomeNavigator from './HomeNavigator';
import ProductNavigator from './ProductNavigator';
import SearchNavigator from './SearchNavigator';
import ProfileNavigator from './ProfileNavigator';
import LoginScreen from '../screens/LoginScreen';
const tabs = [
{
name: 'Home',
screen: HomeNavigator,
},
{
name: 'Coffee',
screen: ProductNavigator,
},
{
name: 'Search',
screen: SearchNavigator,
},
{
name: 'Gift',
screen: PromoScreen,
},
{
name: 'Profile',
screen: LoginScreen,
},
];
const Tab = createBottomTabNavigator();
const TabNavigator = () => {
const offsetAnimation = React.useRef(new Animated.Value(0)).current;
return (
<>
<Tab.Navigator
initialRouteName="Home"
screenOptions={{
headerShown: false,
tabBarShowLabel: false,
}}>
{tabs.map(({name, screen}, index) => {
return (
<Tab.Screen
key={name}
name={name}
component={screen}
options={{
tabBarIcon: ({focused}) => {
return (
<Icon
icon={name}
size={40}
style={{
tintColor: focused ? colors.mainColor : colors.gray,
}}
/>
);
},
}}
listeners={{
focus: () => {
Animated.spring(offsetAnimation, {
toValue: index * (sizes.width / tabs.length),
useNativeDriver: true,
}).start();
},
}}
/>
);
})}
</Tab.Navigator>
</>
);
};
export default TabNavigator;
DrawNavigator
import React from 'react';
import {createDrawerNavigator} from '#react-navigation/drawer';
import {NavigationContainer} from '#react-navigation/native';
import HomeNavigator from './HomeNavigator';
const Drawer = createDrawerNavigator();
const MainDrawNavigator = () => {
return (
<NavigationContainer>
<Drawer.Navigator initialRouteName="Home">
<Drawer.Screen name="Home" component={HomeNavigator} />
</Drawer.Navigator>
</NavigationContainer>
);
};
export default MainDrawNavigator;
Home Navigator
import React from 'react';
import {createSharedElementStackNavigator} from 'react-navigation-shared-element';
import HomeScreen from '../screens/HomeScreen';
const Stack = createSharedElementStackNavigator();
const HomeNavigator = () => {
return (
<Stack.Navigator>
<Stack.Screen
name="HomeScreen"
component={HomeScreen}
options={{
headerShown: false,
useNativeDriver: true,
gestureEnabled: false,
}}
/>
</Stack.Navigator>
);
};
export default HomeNavigator;
HomeScreen
import React from 'react';
import {ScrollView, View} from 'react-native';
import MainHeader from '../components/Header/MainHeader';
import TitleHeader from '../components/Header/TitleHeader';
import MenuSlider from '../components/Home/MenuSlider';
import {Top_Sell, POPULAR} from '../data';
import SectionHeader from '../components/Header/SectionHeader';
import PopularList from '../components/Home/PopularList';
const HomeScreen = () => {
return (
<View style={{flex: 1, backgroundColor: '#fbfbfb'}}>
<MainHeader />
<TitleHeader mainTitle="Gợi ý" secondTitle="cho bạn" />
<ScrollView showsVerticalScrollIndicator={false}>
<MenuSlider list={Top_Sell} />
<SectionHeader title="Phổ biến" buttonTitle="More" onPress={() => {}} />
<PopularList list={POPULAR} />
</ScrollView>
</View>
);
};
export default HomeScreen;
I have tried several ways on stackOverflow but it doesn't work
You need to pass the navigation prop from HomeScreen to MainHeader to use it. Try replacing your HomeScreen with this:
const HomeScreen = ({ navigation }) => {
return (
<View style={{flex: 1, backgroundColor: '#fbfbfb'}}>
<MainHeader navigation={navigation} />
<TitleHeader mainTitle="Gợi ý" secondTitle="cho bạn" />
<ScrollView showsVerticalScrollIndicator={false}>
<MenuSlider list={Top_Sell} />
<SectionHeader title="Phổ biến" buttonTitle="More" onPress={() => {}} />
<PopularList list={POPULAR} />
</ScrollView>
</View>
);
};
please try this one. it seems that there is some issue with navigation state. check this one it will work.
based on your comment you may forgot to install some packages that are mandatory without that navigation won't work properly.
Please check for these.
#react-navigation/drawer,
#react-navigation/native-stack,
react-native-gesture-handler,
react-native-screens,
react-native-pager-view
[react-native-reanimated][1]
first configure these library , clean your project, delete it from device , rebuild and cheers!!
then. try this one
import React from 'react';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import Icon from '../../utils/Icon';
import {View} from 'react-native';
import {spacing} from '../../constants/theme';
import { useNavigation } from '#react-navigation/native';
const MainHeader = ({}) => {
const navigation = useNavigation();
const insets = useSafeAreaInsets();
return (
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignContent: 'center',
paddingHorizontal: spacing.l,
marginTop: insets.top,
}}>
<Icon icon="Hamburger" onPress={() => navigation.openDrawer()} />
<Icon style={{width: 25, height: 25}} icon="Cart" onPress={() => {}} />
</View>
);
};
export default MainHeader;
I'm creating a bottom tabs in react native, which will have two screens Home and News.
But first, user will need to Sign In and the users data will be passed from the login screen to Home screen. How do i pass those data. By using
navigation.navigate('Home', {Name: Name});
I can successfuly retrieve the data in Homescreen, if I just use two screen(Login and Home in a stack). However, when I change to navigate to the tabs(which includes Home and News), it doesnt work with error 'Undefined is not an object(evaluating 'route.params.Name'.
May you guys show me which part did I miss?
Here's the app.js code.
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import HomeScreen from './homescreen';
import NewsScreen from './newsscreen';
import LoginScreen from './loginscreen';
import { NavigationContainer } from '#react-navigation/native';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import { createStackNavigator } from '#react-navigation/stack';
const Tab = createBottomTabNavigator();
function MyTabs() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="News" component={NewsScreen} />
</Tab.Navigator>
);
}
const LoginStack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<LoginStack.Navigator screenOptions={{headerShown: false}}
initialRouteName="Login">
<LoginStack.Screen name="Login"component={LoginScreen}/>
<LoginStack.Screen name="MyTabs" component={MyTabs} />
</LoginStack.Navigator>
</NavigationContainer>
);
}
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Following is the homescreen code:
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default function HomeScreen({route, navigation}) {
var Name = route.params.Name;
return (
<View style={styles.container}>
<Text>{Name}</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
And finally here's the login code:
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';
export default function LoginScreen({navigation}) {
const Name = 'Boy';
const login = () => {
navigation.navigate('MyTabs', {Name: 'Boy'});}
return (
<View style={styles.container}>
<Text>LoginScreen</Text>
<TouchableOpacity
onPress={login}
><Text
>LOGIN</Text>
</TouchableOpacity>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
})
I'm trying to learn how to pass data from a screen to another screen, in which the screen is located inside a tab stack. I hope you guys can understand the question and provide me with your opinion and solution. Thanks.
Output:
It needed just this little modification in MyTabs component:
function MyTabs({ navigation, route }) {
const { name } = route.params;
console.log(name);
return (
<Tab.Navigator>
<Tab.Screen
name="Home"
component={() => <HomeScreen name={route.params.name} />}
/>
<Tab.Screen name="News" component={NewsScreen} />
</Tab.Navigator>
);
}
Here is the working solution:
App.js
import * as React from 'react';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import HomeScreen from './home';
import NewsScreen from './newscreen';
import LoginScreen from './login';
// You can import from local files
const Tab = createBottomTabNavigator();
function MyTabs({ navigation, route }) {
const { name } = route.params;
console.log(name);
return (
<Tab.Navigator>
<Tab.Screen
name="Home"
component={() => <HomeScreen name={route.params.name} />}
/>
<Tab.Screen name="News" component={NewsScreen} />
</Tab.Navigator>
);
}
const LoginStack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<LoginStack.Navigator
screenOptions={{ headerShown: true }}
initialRouteName="Login">
<LoginStack.Screen name="Login" component={LoginScreen} />
<LoginStack.Screen name="MyTabs" component={MyTabs} />
</LoginStack.Navigator>
</NavigationContainer>
);
}
export default App;
LoginScreen
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import React from "react";
export default function LoginScreen({ navigation }) {
const Name = 'Name From Login Screen';
const login = () => {
console.log("hi");
navigation.navigate('MyTabs', { name : Name });
}
return (
<View style={styles.container}>
<View style={styles.bottomView}>
<TouchableOpacity onPress={login} style={styles.button}>
<Text style={styles.textStyle}>LOGIN</Text>
</TouchableOpacity>
</View>
</View>
);
}
Home.js
import { Text, View, StyleSheet } from 'react-native';
import React from "react";
export default function HomeScreen({route, navigation, name}) {
console.log("***",name)
return (
<View style={styles.container}>
<Text style={styles.name}>{name}</Text>
</View>
);
}
Working Expo Snack example.
It's a little complicated to explain react native screens once you start combining bottomtabnavigator and stack navigator and even drawernavigation.
you may need to create stack navigation inside tab navigation like this.
//creating a separate stack within your tab
const HomeStack = createStackNavigator()
const HomeStackNavigator = () => {
return (
<HomeStack.Navigator screenOptions={{headerShown: false}}
initialRouteName="HomeScreen">
<HomeStack.Screen name="Home"component={HomeScreen}/>
</HomeStack.Navigator>
)
}
const Tab = createBottomTabNavigator();
function MyTabs() {
return (
<Tab.Navigator initialRouteName="HomeStackNavigator">
<Tab.Screen name="HomeStackNavigator" component={HomeStackNavigator} />
<Tab.Screen name="News" component={NewsScreen} />
</Tab.Navigator>
);
}
I believe, different navigators doesn't really talk to each other well. If you are using different navigator be prepare to nest stack navigators inside them.
The idea is, the parent of each display component should be a stack navigator. This will also allow you to better control your screenOptions.
I want to update the text after clicking button.
But couldn't find a way to do this.
The code is like:
import * as React from 'react';
import { View, Button, Text } from 'react-native';
import { NavigationContainer, CommonActions } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
function HomeScreen({ navigation }) {
const state = {
user: 'test',
};
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>{state.user}'s profile</Text>
<Button
title="Change user param"
onPress={() =>
navigation.dispatch({
...CommonActions.setParams({ user: 'Wojtek' }),
})
}
/>
</View>
);
}
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
Expo example
I've tried navigation.setParams() and this.setState(), they couldn't work with the code I attached above.
You have to use hooks in functional components. So the code would be something like this:
import * as React from 'react';
import { View, Button, Text } from 'react-native';
import { NavigationContainer, CommonActions } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
function HomeScreen({ navigation }) {
const [user, setUser] = React.useState('test');
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>{user}'s profile</Text>
<Button
title="Change user param"
onPress={() => setUser("keidakira") }
/>
</View>
);
}
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
Check it's working at Expo Link
I have an icon on the right side of my Header. When pressed I want to be transferred to another page. However, it comes up with an error.
This is my 'icon' screen:
import React, { Component } from 'react';
import { StyleSheet, View, Text, Image, TouchableOpacity } from 'react-native';
const Login = props => {
return (
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity
onPress={() => {
props.navigation.navigate({routeName: 'Login'});}}>
<Image
source={{
uri:
'https://clipartart.com/images/login-icon-clipart-5.jpg',
}}
style={{
width: 40,
height: 40,
borderRadius: 40 / 2,
marginLeft: 15,
}}
/>
</TouchableOpacity>
</View>
);
}
export default Login;
This is my 'navigation' screen:
import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer} from 'react-navigation';
import React from 'react';
import Homepage from './screens/Homepage';
import Checkoutpage from './screens/Checkoutpage';
import Filterpage from './screens/Filterpage';
import Locationpage from './screens/Locationpage';
import Menupage from './screens/MenuPage';
import Welcomepage from './screens/Welcomepage';
import Loginpage from './screens/Loginpage';
import Finalpage from './screens/Finalpage';
import Login from './Components/Login';
const Navigation = createStackNavigator({
Home:Homepage,
Checkout: Checkoutpage,
Filter: Filterpage,
Location: Locationpage,
Menu: Menupage,
Welcome: Welcomepage,
Login: Loginpage,
Final: Finalpage
},
{
defaultNavigationOptions: {
headerRight:() => <Login/>
}
}
);
I'm very new to react-native. So if you found the problem, can you please explain thoroughly so I understand. Thank you!!
So it looks like you are expecting the navigation object to be part of the props passed to your <Login/> component. This object is only defined for screen components in react-navigate.
This means that you need to get access to the navigation functionality some other way. Luckily, this library provides you with the useNavigation() hook. So using that in your component would look something like:
// react-navigation v5+
import { useNavigation } from '#react-navigation/native';
const Login = () => {
const navigation = useNavigation();
return (
<View style={{ flexDirection: "row" }}>
<TouchableOpacity
onPress={() => {
navigation.navigate({ routeName: "Login" });
}}
>
<Image
source={{
uri: "https://clipartart.com/images/login-icon-clipart-5.jpg",
}}
style={{
width: 40,
height: 40,
borderRadius: 40 / 2,
marginLeft: 15,
}}
/>
</TouchableOpacity>
</View>
);
};
It seems to me you are using React Navigation v4.x , in order to use the useNavigation hook you need to upgrade to v5.x.
The navigation prop will be passed to all screens by default and you can use the useNavigation hook like #faelks suggested (if needed in other components).
UPGRADE TO v5 FIRST.
Here you have a little example for v5.x version:
import React from 'react'
import { Button, View, StyleSheet } from 'react-native'
import { NavigationContainer } from '#react-navigation/native'
import { createStackNavigator } from '#react-navigation/stack'
const Home = ({ navigation }) => (
<View style={styles.component}>
<Button title="Go to login" onPress={() => navigation.navigate('Login')} />
</View>
)
const Login = ({ navigation }) => (
<View style={styles.component}>
<Button title="Go back" onPress={() => navigation.goBack()} />
</View>
)
const Main = createStackNavigator()
const mainConfig = {
// configuration for this stack
initialRouteName: "Home",
}
export default props => (
<NavigationContainer>
<Main.Navigator {...mainConfig}>
<Main.Screen name="Home" component={Home} />
<Main.Screen name="Login" component={Login} />
{/* Other screens for this stack */}
</Main.Navigator>
</NavigationContainer>
)
const styles = StyleSheet.create({
component: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
}
})
I am trying to create new drawer navigation but getting this below error
Unable to resolve "react-native-screens" from "node_modules\#react-navigation\drawer\src\views\DrawerView.tsx"
Failed building JavaScript bundle.
but same code i tried in new empty project it worked with react-native-drawer in older version of "react-navigation": "^2.6.2" but the same not working in "react-navigation": "^4.0.10", it shows that react-native-drawer is removed and #react-native/drawer is latest so we need to use that but its not working kindly help me resolving this...Code is in below
import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createDrawerNavigator } from '#react-navigation/drawer';
function Feed() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Feed Screen</Text>
</View>
);
}
function Article() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Article Screen</Text>
</View>
);
}
const Drawer = createDrawerNavigator();
function MyDrawer() {
return (
<Drawer.Navigator>
<Drawer.Screen name="Feed" component={Feed} />
<Drawer.Screen name="Article" component={Article} />
</Drawer.Navigator>
);
}
export default function DrawerNavigator() {
return (
<NavigationContainer>
<MyDrawer />
</NavigationContainer>
);
}
Yup! this is old question but I want to answer for this. However we know that react native packages varies based on version updates. Follow the below steps if your using (version)V5x I am sure it works.
npm install --save react-navigation-drawer (Install Packages)
import { createDrawerNavigator } from 'react-navigation-drawer'; (Import Navigator using 'react-navigation-drawer')
npm install #react-navigation/drawer
or
npm install #react-navigation/drawer
To use this drawer navigator, import it from #react-navigation/drawer:
import { createDrawerNavigator } from '#react-navigation/drawer';
Add react-native-reanimated/plugin in babel.config.js file
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
Plugin: ['react-native-reanimated/plugin'],
};
I know this is not a right solution but anyway it worked for me...
instead of #react-navigation/drawer we can use
import { createDrawerNavigator,DrawerItems} from 'react-navigation-drawer';
this works and I have pasted my working code below
In ReactNative update they moved createDrawerNavigator and DrawerItems from react-navigation to react-navigation-drawer...
import React from 'react';
import { StyleSheet, Text, View, SafeAreaView, ScrollView,Dimensions,Image } from 'react-native';
import { createAppContainer} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';
import { createDrawerNavigator,DrawerItems} from 'react-navigation-drawer';
import Login from '../screens/Login';
import Home from '../screens/Home';
const CustomDrawComponent=(props) => (
<SafeAreaView style={{flex:1}}>
<View style={{paddingTop:45, backgroundColor:'green'}}>
<View style={{height:150,alignItems:'center', justifyContent:'center'}}>
<Image source={require('../assets/Logos/userPic.png')}
style={{height:120,width:120,borderRadius:60}} />
</View>
</View>
<ScrollView>
<DrawerItems {...props}/>
</ScrollView>
<Text style={{paddingBottom:100, paddingLeft:65}}>Vision Cultura V1.0</Text>
</SafeAreaView>
)
const screens = createDrawerNavigator({
Main: {
screen: createStackNavigator({
Home: {screen: Home},
Login: {screen: Login},
}, {initialRouteName: "Login"})
},
Home: Home,
Login: Login
},
{
contentComponent:CustomDrawComponent
});
const index = createAppContainer(screens);
export default index;