android - React Native - 导航到屏幕 - 无效的 Hook 调用

标签 android ios react-native react-native-android react-native-ios

应用程序.js:

import React, { Component } from 'react';
import {View,Text} from 'react-native';
import { createDrawerNavigator } from 'react-navigation-drawer';
import {createAppContainer} from 'react-navigation';

import Epics from './screens/tmp';
import Pager from './screens/Pager';

const DrawerNavigator = createDrawerNavigator({
 Home: {screen: Epics},
 Page: {screen: Pager}
},{initialRouteName: 'Home'});

const Stack = createAppContainer(DrawerNavigator);

export default class App extends Component {
  render() {
    return <Stack />;
  }
}
尝试通过自定义组件导航到屏幕:
import { ActivityIndicator, Image, StyleSheet, View, TouchableHighlight } from 'react-native';
import PropTypes from 'prop-types';
import React from 'react';
import { useNavigation } from 'react-navigation-hooks';

import AuthorRow from './AuthorRow';

export default class Card extends React.Component {

  static propTypes = {
    fullname: PropTypes.string.isRequired,
    image: Image.propTypes.source.isRequired,
    linkText: PropTypes.string.isRequired,
    onPressLinkText: PropTypes.func.isRequired,
    epicId: PropTypes.string.isRequired,
  };

  state = {
    loading: true,
  };

  getNav(){
    return useNavigation();
  }

  handleLoad = () => {
    this.setState({ loading: false });
  };

  render() {
    const { fullname, image, linkText, onPressLinkText, epicId, prop } = this.props;
    const { loading } = this.state;
    
    return (
      <View>
        <AuthorRow
          fullname={fullname}
          linkText={linkText}
          onPressLinkText={onPressLinkText}
        />
        <TouchableHighlight onPress={() => this.getNav().navigate('Pager')} underlayColor="white">
          <View style={styles.image}>
            {loading && (
              <ActivityIndicator style={StyleSheet.absoluteFill} size={'large'} />
            )}
            <Image
              style={StyleSheet.absoluteFill}
              source={image}
              onLoad={this.handleLoad}
            />
          </View>
        </TouchableHighlight>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  image: {
    aspectRatio: 1,
    backgroundColor: 'rgba(0,0,0,0.02)',
  },
});
从上面的代码,尝试使用导航:
从'react-navigation-hooks'导入{useNavigation};
getNav(){ 返回使用导航(); }
this.getNav().navigate('寻呼机')
它错误我们的陈述:不变违规:无效的 Hook 调用。钩子(Hook)只能在函数组件的主体内部调用。
如何使用“useNavigation()”导航或如何在组件中获取“this.props.navigation”引用? (我是 React Native 新手,请帮我理解)
更新
我尝试使用“this.props.navigation”,它给了我未定义的错误:
enter image description here

最佳答案

    //In Card component remove this
    //import { useNavigation } from 'react-navigation-hooks';

    //Add this line
    import {withNavigation} from 'react-navigation';

    class Card extends React.Component {

      static propTypes = {
        fullname: PropTypes.string.isRequired,
        image: Image.propTypes.source.isRequired,
        linkText: PropTypes.string.isRequired,
        onPressLinkText: PropTypes.func.isRequired,
        epicId: PropTypes.string.isRequired,
      };

      state = {
        loading: true,
      };

    //Remove this
    //  getNav(){
     //   return useNavigation();
      //}

      handleLoad = () => {
        this.setState({ loading: false });
      };

      render() {
        const { fullname, image, linkText, onPressLinkText, epicId, prop } = this.props;
        const { loading } = this.state;

        return (
          <View>
            <AuthorRow
              fullname={fullname}
              linkText={linkText}
              onPressLinkText={onPressLinkText}
            />
            <TouchableHighlight onPress={() =>  
//Make change like this
 this.props.navigation.navigate('Pager')} underlayColor="white">
              <View style={styles.image}>
                {loading && (
                  <ActivityIndicator style={StyleSheet.absoluteFill} size={'large'} />
                )}
                <Image
                  style={StyleSheet.absoluteFill}
                  source={image}
                  onLoad={this.handleLoad}
                />
              </View>
            </TouchableHighlight>
          </View>
        );
      }
    }

    const styles = StyleSheet.create({
      image: {
        aspectRatio: 1,
        backgroundColor: 'rgba(0,0,0,0.02)',
      },
    });
//Make change like this
    export default withNavigation(Card)

关于android - React Native - 导航到屏幕 - 无效的 Hook 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60700905/

相关文章:

android - 通过 Intent 向多个电话号码发送短信

ios - 初始化 NSPersistentStore 对象

ios - 在世博会上发布 react native 应用程序时出现意外代码 404

ios - 表格 View 之后的表格 View

react-native - 与 ionic 框架 react 的导航问题

Android:在模拟器上安装 Market 应用程序?

android - Android 的 HTTP CONNECT 方法

javascript - 无法从 React Native 中的父组件调用子组件方法

javascript - React Native - 如何在不刷新应用程序的情况下实时收听 AsyncStorage 更改(条件屏幕渲染)?

android - 错误 MSB4064 : The "AllowUnsecureUrls" parameter is not supported by the "XamarinDownloadArchives" task