TypeError: undefined is not an object (evaluating 'navigation.push') React Native

时间:2021-02-13 13:07:43

标签: react-native react-navigation

所以这是错误:TypeError: undefined is not an object (evaluating 'navigation.push')

我使用 React Navigation 5 和。这是我的 App.js:

import React, { useState, useEffect } from 'react';
import { Text, View, StyleSheet, TouchableOpacity, FlatList, Alert} from 'react-native';

const MyApp = ({navigation}) => {
  return(
    <Button
      onPress={() => navigation.push('Information')}
      title="Get Information"
    />
  );
}

export default MyApp;

有什么问题? :)

1 个答案:

答案 0 :(得分:0)

您应该在 const navigation 中定义 MyApp! 你也应该导入 useNavigation

import React, { useState, useEffect } from 'react';
import { Text, View, StyleSheet, TouchableOpacity, FlatList, Alert} from 'react-native';
import { useNavigation } from '@react-navigation/native';

const MyApp = () => {
  const navigation = useNavigation(); 
  return(
    <Button
      onPress={() => navigation.push('Information')}
      title="Get Information"
    />
  );
}

export default MyApp;

感谢您的帮助Allandiego