react-native-swiper doesnt support this.props.navigation.navigate - react-native

I am new to react native, I am trying to implement react-native-swiper to my project
render() {
return (
<Swiper
loop={false}
showsPagination={false}
index={1}>
<View style={this.viewStyle()}>
<ActivityScreen />
</View>
<Swiper
horizontal={true}
loop={false}
showsPagination={false}
index={1}>
<View style={this.viewStyle()}>
<YourGroups />
</View>
</Swiper>
<View style={this.viewStyle()}>
<AlertScreen />
</View>
</Swiper>
)
}
}
But when I am in component screen and click on the button
<Button title="thank you" onPress={()=>this.props.navigation.navigate("ThankScreen")} ></Button>
I get an error
undefined is not an object (evaluating 'this.props.navigation.navigate')

I ran in to the same issue and fixed it using React Navigation, 5 which allows you to access the navigation prop from any component using the useNavigation hook:
import React from 'react';
import { Button } from 'react-native';
import { useNavigation } from '#react-navigation/native';
const GoToButton = ({ screenName }) => {
const navigation = useNavigation();
return (
<Button
title={`Go to ${screenName}`}
onPress={() => navigation.navigate(screenName)}
/>
);
}

Related

React Navigation goBack()

I got some error when i have used goBack(null) method in functional component in react navigation
function Header({ titleText, navigation}) {
return (
<Appbar.Header style={styles.headerContainer}>
<Appbar.BackAction style={styles.backButton} onPress={() => navigation.goBack(null)} >
<Text style={styles.back}></Text>
</Appbar.BackAction>
<View style={styles.container}>
<Title style={styles.title}>{titleText}</Title>
</View>
</Appbar.Header>
)}
Try with useNavigation hook from #react-navigation/native
import * as React from 'react';
import { Text } from 'react-native';
import { useNavigation } from '#react-navigation/native';
function MyText() {
const navigation = useNavigation();
return <Text onPress={() => navigation.goBack()}>Go Back</Text>;
}

How to open a drawer by clicking hamburger icon in header in react native?

This is my code:
import React from 'react';
import { Text, Block } from 'galio-framework';
import { StyleSheet } from 'react-native';
import { Ionicons } from '#expo/vector-icons';
export default function Header(props) {
return (
<Block style={ styles.header }>
<Block>
<Ionicons name="md-menu" size={32} color="grey" onPress={() => navigation.openDrawer()} />
<Text style={ styles.title } p>{ props.title }</Text>
</Block>
<Block>
<Ionicons name="md-search" size={32} color="grey" />
</Block>
</Block>
)
}
When clicking on the hamburger icon I get this error:
RefererenceError: Can't find variable: Navigation
If I change my code to:
onPress={() => this.props.navigation.openDrawer()}
I get this error:
TypeError: undefined is not an object (evaluating '_this.props)
This is one of my files where I am importing Header:
import React, { Component } from 'react';
import { Text, Block, Input, Button, Card } from 'galio-framework';
import { StyleSheet, ScrollView, View } from 'react-native';
import Header from '../../common/Header';
class Accountant extends Component {
render() {
return (
<Block style={ styles.blockStyle }>
<Header title="Accounts" />
<Button onlyIcon icon="right" onPress={() => this.props.navigation.navigate('Projects')}>
</Button>
</Block>
);
}
}
I am using nested navigation. Here is my App.js file:
const Stack = createStackNavigator();
const DrawerAccountant = createDrawerNavigator();
function AccountantDrawer() {
return (
<DrawerAccountant.Navigator initialRouteName="Accountant">
<DrawerAccountant.Screen name="Accountant" component={AccountantScreen} />
<DrawerAccountant.Screen name="My Account" component={MyAccountScreen} />
</DrawerAccountant.Navigator>
);
}
function App() {
return (
<NavigationContainer>
<Stack.Screen name="Accountant" component={AccountantDrawer} />
<Stack.Screen name="Projects" component={ProjectsScreen} />
<Stack.Screen name="Tasks" component={TasksScreen} />
<Stack.Screen name="My Account" component={MyAccountScreen} />
</Stack.Navigator>
</NavigationContainer>
)
}
export default App;
The navigation drawer is working in the screens: "Accountant" and "My Account". The drawer opens when I click the menu icon in header. But it's not working in the screens: "Projects" and "Tasks".
Pass your navigation props to child component
<Header title="Accounts" navigation={this.props.navigation}/>
then you can access your navigation from Header as below
<Ionicons name="md-menu" size={32} color="grey" onPress={() => props.navigation.openDrawer()} />
or you can use withNavigation
Hope this helps you. Feel free for doubts.

React Native - Navigation with functional components

I installed Navigation to take some data from a page and send to another (hopefully I will).
But it doesn't work and I get
"We couldn't find a navigation object. Is your component inside a navigator?"
File Insert.js
import React, { useState } from "react";
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
import { useNavigation } from "#react-navigation/native";
import ViewData from "./ViewData";
const MainNavigator = createStackNavigator({
ViewDataPage: ViewData
});
...
const Insert = props => {
...
createAppContainer(MainNavigator);
const navigate = useNavigation();
return (
<TouchableWithoutFeedback
onPress={() => {
Keyboard.dismiss();
}}
>
<View style={styles.inputContainer}>
<Input
placeholder="Your Name"
onChangeText={text => setEnteredName(text)}
value={enteredName}
/>
...
<View>
<Button
title="Submit"
onPress={() => sendValues(enteredName, enteredSurname)}
/>
<Button
title="Click"
onPress={() =>navigate("ViewDataPage", { name: "Jane" })
}
/>
</View>
</View>
</TouchableWithoutFeedback>
I think I made a mess, I can't even get the page "ViewData".
I would like to change page sending some values in the hooks
Any idea where I mistook?
"#react-navigation/native" is for React Navigation 5. You can't use it with React Navigation 4.

How do you solve: undefined is not an object (evaluating 'this.panResponder.panHandlers')?

Hi my friend how to solve this problem,I have the following sourcode :
import React, { Component } from 'react';
import { Container, Header, Content, SwipeRow, View, Text, Icon, Button } from 'native-base';
class SwipeRowExample extends Component {
render() {
return (
<Container>
<Header />
<Content scrollEnabled={false}>
<SwipeRow
leftOpenValue={75}
rightOpenValue={-75}
left={
<Button success onPress={() => alert('Add')}>
<Icon active name="add" />
</Button>
}
body={
<View>
<Text>SwipeRow Body Text</Text>
</View>
}
right={
<Button danger onPress={() => alert('Trash')}>
<Icon active name="trash" />
</Button>
}
/>
</Content>
</Container>
);
}
}
export default SwipeRowExample;
Message obtained: undefined is not an object (evaluating 'this.panResponder.panHandlers')
But the results displayed :
SwipeRow was removed from the latest version and they recommend react-native-swipe-list-view
to use SwipeRow change native base version to 2.12.0
SwipeRow (removed)

How to add custom drawer in drawerNavigator, react navigation 4.x?

I'm having problem with custom drawer navigator. all the methods on internet address lower versions of react navigation which do not work anymore.
here's my code
import { createAppContainer,DrawerItems } from 'react-navigation';
import HomeScreen from './screens/HomeScreen';
import SettingsScreen from './screens/SettingsScreen';
import {SafeAreaView,ScrollView,Dimensions,View} from 'react-native';
const MyDrawerNavigator = createDrawerNavigator({
Home: HomeScreen,
Settings: SettingsScreen,
},{
contentComponent:CustomDrawerComponent,
})
const CustomDrawerComponent = (props) => {
<SafeAreaView style={{flex:1}}>
<View>
<Image source={{'uri' : 'https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569_960_720.jpg'}} />
</View>
<ScrollView>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
}
const AppContainer = createAppContainer(MyDrawerNavigator);
export default AppContainer;
the code works fine without custom drawer. but when i add custom drawer the links on the sidebar do not appear. the side bar is empty.
You need to change your import to import from react-navigation-drawer.
If you're using react-navigation-drawer 1.x:
import { DrawerItems } from 'react-navigation-drawer';
If you're using react-navigation-drawer 2.x, use DrawerNavigatorItems instead:
import { DrawerNavigatorItems as DrawerItems } from 'react-navigation-drawer';
Always read the official docs: https://reactnavigation.org/docs/en/drawer-navigator.html#providing-a-custom-contentcomponent
Instead of using this
const CustomDrawerComponent = (props) => {
<SafeAreaView style={{flex:1}}>
<View>
<Image source={{'uri' : 'https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569_960_720.jpg'}} />
</View>
<ScrollView>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
}
i created a new react component
class CustomDrawerComponent React.Componect{
render(){
return(
<SafeAreaView style={{flex:1}}>
<View>
<Image source={{'uri' : 'https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569_960_720.jpg'}} />
</View>
<ScrollView>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
)
}
}
this worked