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'
Related
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
I want to use navigation in a class component, which is not a screen component and does not automatically access navigation via props, So I have to pass the navigation to it as props. But the parent component is a functional component And I used navigation hooks in it.
However, how can I use navigation in the child component?
ParentComponent.js
import React from 'react';
import {View , TouchableOpacity} from 'react-native';
import {useNavigation} from 'react-navigation-hooks';
import ChildComponent from './ChildComponent';
const ParentComponent =()=>{
const {navigate} = useNavigation();
return(
<View>
<TouchableOpacity onPress={() => navigate('AnotherScreen')} />
<ChildComponent />
</View>
)
}
export default ParentComponent;
ChildComponent.js
import React , {Component} from 'react';
import {View , TouchableOpacity} from 'react-native';
class ChildComponent extends Component {
render(){
return(
<View>
<TouchableOpacity onPress={() => ???}>
<Text>sample text</Text>
</TouchableOpacity>
</View>
)
}
}
export default ChildComponent;
I use the following dependencies:
"dependencies": {
"#react-native-community/masked-view": "^0.1.10",
"native-base": "2.13.8",
"react": "16.13.1",
"react-native": "0.63.0",
"react-native-gesture-handler": "^1.6.1",
"react-navigation": "^4.0.7",
"react-navigation-drawer": "^2.3.3",
"react-navigation-hooks": "^1.1.0",
"react-navigation-stack": "^1.8.1",
},
Your guidance is highly appreciated.
If you have access to navigation prop in parent component simply pass it to the child navigate using the prop.
const ParentComponent =()=>{
const {navigate} = useNavigation();
return(
<View>
<TouchableOpacity onPress={() => navigate('AnotherScreen')} />
<ChildComponent navigate={navigate}/>
</View>
)
}
class ChildComponent extends Component {
render(){
return(
<View>
<TouchableOpacity onPress={() => this.props.navigate('screenname')}>
<Text>sample text</Text>
</TouchableOpacity>
</View>
)
}
}
You must be getting navigation inside props of the parent component.
In parent component:
const ParentComponent =(props)=>{
const {navigate} = useNavigation();
return(
<View>
<TouchableOpacity onPress={() => navigate('AnotherScreen')} />
<ChildComponent navigation={props.navigation} />
</View>
)
}
In the child component, you can access navigation:
class ChildComponent extends Component {
constructor(props) {
super(props);
}
render() {
return (
<View>
<TouchableOpacity onPress={() => this.props.navigation.navigate('SomeOtherScreen')}>
<Text>sample text</Text>
</TouchableOpacity>
</View>
)
}
}
I created a drawer menu using the DrawerNavigation feature of react-navigation. I wanted to create a button that would close the menu but the onPress function seems to not work
I've imported DrawerActions from 'react-navigation-drawer' and I've tried using different syntaxes
ex.) this.props.navigation.dispatch(DrawerActions.closeDrawer());
this.props.navigation.closeDrawer();
import React, { Component } from 'react';
import {
Image,
StyleSheet,
Text,
ImageBackground,
TouchableWithoutFeedback,
View,
Button,
ScrollView,
} from 'react-native';
import {
createDrawerNavigator,
createStackNavigator,
createAppContainer,
DrawerItems,
SafeAreaView,
NavigationActions
} from 'react-navigation';
import { DrawerActions, } from 'react-navigation-drawer';
const navigator = createDrawerNavigator(
{
Home: Lander,
Page1: Lander,
Page2: Lander,
Page3: Lander,
Page4: Lander,
},
{
contentComponent: (props) => (
<SafeAreaView>
<View style= {{backgroundColor:'black'}}>
<TouchableWithoutFeedback onPress={() => this.props.navigation.dispatch(DrawerActions.closeDrawer())}>
<Image source={require('./Images/x.png')} style = {styles.cross}/>
</TouchableWithoutFeedback>
</View>
<ScrollView style= {{backgroundColor: 'black', paddingLeft: '5%'}}>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
)
},
);
I ultimately want to be able to click on the x button and let it redirect me to the home screen.
I'm getting the following error
Undefined is not an object (evaluating '_this.props.navigation')
You are importing DrawerActions from the wrong package.
Change
import { DrawerActions, } from 'react-navigation-drawer';
To
import { DrawerActions } from 'react-navigation'
And to close it, you do it like
onPress={() => this.props.navigation.dispatch(DrawerActions.openDrawer())}
Edit:
What you are doing wrong is that you get this.props but it's only props.
contentComponent: (props) => (
<SafeAreaView>
<View style= {{backgroundColor:'black'}}>
{// changed to props without this}
<TouchableWithoutFeedback onPress={() => props.navigation.dispatch(DrawerActions.closeDrawer())}>
<Image source={require('./Images/x.png')} style = {styles.cross}/>
</TouchableWithoutFeedback>
</View>
<ScrollView style= {{backgroundColor: 'black', paddingLeft: '5%'}}>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
)
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import { gestureHandlerRootHOC } from 'react-native-gesture-handler'
AppRegistry.registerComponent(appName, () => gestureHandlerRootHOC(App));
hope is helps
returning this error while tying to navigate using switchnavigation.
i have tried removing this. props then returns undefined 'navigation'.
import React from 'react';
import { Text, View, Image, TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import { createSwitchNavigator, createAppContainer , withNavigation } from 'react-navigation';
import {widthPercentageToDP as wp, heightPercentageToDP as hp} from 'react-native-responsive-screen';
import layout from '../../constants/LayoutStyle'
import QuoteScreen from './QuoteScreen';
const HomeScreen = () => {
return (
<View style={styles.viewStyles}>
<View style={{position: 'absolute',top: hp('50%')+wp('37.5%'),left:wp('15%'),width: wp('32.5%'),height:
wp('32.5%'),backgroundColor:'rgba(255,255,255,0.1)'}}>
<View style={{alignItems: 'center',justifyContent: 'center',flex:1}}>
<Icon name="ios-book" color="purple" size={wp('10%')}
onPress={() => this.props.navigation.navigate('Quote')}
/>
<Text style={styles.tabTextStyle}>Books</Text>
</View>
</View>
);
};
const RootStack = createSwitchNavigator(
{
Home: HomeScreen,
Quote: QuoteScreen,
}
);
const AppContainer = createAppContainer(RootStack);
export default class app extends React.Component {
render() {
return <AppContainer />
}
}
expected to complete navigation properly
HomeScreen is a functional component and hence you should not use this.props.navigation, just say props.navigation.
And If you want to use props inside the function component, then use should pass props as an argument to that functional component. Like this =>
const HomeScreen = (props) => {
return (
<View style={styles.viewStyles}>
<View style={{position: 'absolute',top:
hp('50%')+wp('37.5%'),left:wp('15%'),width: wp('32.5%'),height:
wp('32.5%'),backgroundColor:'rgba(255,255,255,0.1)'}}>
<View style={{alignItems: 'center',justifyContent: 'center',flex:1}}>
<Icon name="ios-book" color="purple" size={wp('10%')}
onPress={() => this.props.navigation.navigate('Quote')}
/>
<Text style={styles.tabTextStyle}>Books</Text>
</View>
</View>
);
};
If this does not work then pass navigation as props to HomeScreen component wherever u use, Like this =>
<HomeScreen
navigation = {this.props.navigation} // or navigation = {props.navigation}, if it is functional component
/>
I'm using Drawer Navigator from React Navigation but after creating the custom component, the navigation doesn't work. There is no issue regarding importing.
Here is the Code:-
import React, { Component } from 'react';
import {DrawerNavigator} from 'react-navigation';
import {StyleSheet,Text,View,ScrollView,Image,Dimensions,TouchableOpacity} from 'react-native';
import Ionicons from 'react-native-vector-icons/Ionicons';
import HomeScreen from './components/HomeScreen/HomeScreen';
import Home from './components/Home/Home';
import Sample from './Sample';
const{height,width}=Dimensions.get('window');
const CustomDrawer = (props) => {
return(
<View>
<View style={styles.list}>
<TouchableOpacity onPress={()=>this.props.navigation.navigate('Sample')}>
<View style={styles.listElements}>
<Ionicons name="md-home" size={25} color={'black'} style={styles.listIcons} />
<Text style={styles.listText}>Home</Text>
</View>
</TouchableOpacity>
</View>
</View>
)
}
const Drawer = DrawerNavigator({
HomeScreen: {
screen: HomeScreen
},
Home:{
screen:Home,
},
Sample:{
screen:Sample,
}
},
{
drawerWidth: 350,
contentComponent:CustomDrawer
});
export default class App1 extends React.Component {
render() {
return <Drawer />;
}
}
It throws an "Undefined is not an object (Evaluating '_this.props.navigation')" error clicking the view.