【发布时间】:2020-10-07 12:46:08
【问题描述】:
我很想知道如何使用组件内的useNavigation() 函数从 React Navigation 模块在屏幕之间导航。根据文档,您必须通过在我的标签(此处)中包含 onPress 来包含 useNavigation() 函数。问题 是它向我显示以下错误:'不变违规:无效调用。 Hook 只能在函数组件的主体内部调用。'
我的 React Native 组件:
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>
)
}
}
谢谢。 问候,
昆汀
【问题讨论】:
标签: javascript react-native components react-navigation