I would have liked to know how to navigate from screen to screen from the React Navigation module using the useNavigation() function inside of a component. According to the documentation, you must include the useNavigation() function by including an onPress inside my tag (here ). The problem is that it shows me the following error: 'Invariant violation: Invalid call. Hook can only be called inside of the body of a function component.'
My React Native Component :
import React, { Component } from 'react';
import {View, StyleSheet, Image, TouchableOpacity} from "react-native";
import { Text } from 'react-native-elements';
import { LinearGradient } from 'expo-linear-gradient';
import PropTypes from 'prop-types';
import { useNavigation } from '@react-navigation/native';
export default class HorizontalCard extends Component {
static propTypes = {
screen: PropTypes.string,
title: PropTypes.string,
desc: PropTypes.string,
img: PropTypes.string,
}
render() {
const navigation = useNavigation();
return (
<TouchableOpacity onPress={() => navigation.navigate(this.props.screen)} style={styles.container}>
<View style={styles.card_discord}>
<Image style={styles.card_discord_img} source={{uri: this.props.img}} />
<LinearGradient
start={[1.0, 0.5]}
end={[0.0, 0.5]}
colors={['rgba(42, 159, 255, 0.2)', '#333333', '#333333']}
style={styles.FadeAway}>
<View style={styles.FadeAway}>
<Text h4 style={styles.FadeAway_h2}>{this.props.title}</Text>
<Text style={styles.FadeAway_p}>{this.props.desc}</Text>
</View>
</LinearGradient>
</View>
</TouchableOpacity>
)
}
}
Thanks. Regards,
Quentin