React Native - 导航到屏幕 - 无效的挂钩调用

React Native - Navigate to screen - Invalid hook call

App.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(){ return useNavigation(); }

this.getNav().导航('Pager')

我们的陈述错误:不变违规:无效的钩子调用。钩子只能在函数组件的内部调用。

如何使用“useNavigation()”导航或如何在组件中获取“this.props.navigation”引用? (我是React Native新手,请帮我理解)

更新

我尝试使用“this.props.navigation”,但出现未定义错误:

您不能在 Class 组件中使用挂钩将您的 class 组件转换为功能组件或通过将导航道具传递给您的组件来使用导航对象进行导航 this.props.navigation.navigate('screenName')

这是你的错误

错误

onPress={() => this.getNav().navigate('Pager')

替换为

onPress={() => this.props.navigation.navigate('Pager')

这是例子

https://reactnavigation.org/docs/navigating/

https://medium.com/@marizu_makozi/navigating-between-screens-or-activities-using-react-navigation-library-68d57657d81

另一个示例代码

class FirstScreen extends React.Component {
  static navigationOptions = {
    title: 'First',
  }
  componentDidMount(){
    const {navigate} = this.props.navigation;
    navigate('Second');
    fetch('http://apiserver').then( (response) => {
      navigate('Second');
    });
  }
  render() {
    return (
      <AppLogo />
    );
  }
}

const AppNavigator = StackNavigator({
  First: {
    screen: FirstScreen,
  },
  Second: {
    screen: SecondScreen,
  },
});
    //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)

@sayog 你这是在破坏勾哥的规矩。 你能试试这个吗?

const [loading, setLoading] = useState(false); const navigation = useNavigation();

确保你把它放在之后你的州声明

注意: Hook不要使用class组件。将功能组件用于 hook