导航未定义

时间:2019-09-03 20:31:25

标签: react-native

我想知道是否有人可以帮助我。我在其他文件夹中创建了图片库,并尝试在个人资料页面上调用它。不幸的是,我收到“未定义导航”错误消息。它发生在我的onPress函数上,您可以在下面看到它。我导入了{withNavigation},并且将我的图片库组件导出为以下内容: 使用Navigation(ImageGallery);导出默认值。

有人知道为什么会这样吗?

import React from "react";

import {
  View,
  Text,
  StyleSheet,
  Image,
  Button,
  ImageBackground
} from "react-native";

import ImageGallery from "./ImageGallery";

import { withNavigation } from "react-navigation";

class CharacterProfiles extends React.Component {
  static navigationOptions = {
    title: "The Simpsons",
    headerStyle: {
      backgroundColor: "#53b4e6"
    },
    headerTintColor: "#f6c945",
    headerTitleStyle: {
      fontWeight: "bold"
    },
    headerRight: (
      <Button
        onPress={() => navigation.navigate("ImageGallery")}
        title="Gallery"
        color="#f6c945"
      />
    )
  };

1 个答案:

答案 0 :(得分:1)

您需要按照文档[1]中的说明为导航选项使用动态配置,才能访问navigation对象:

class CharacterProfiles extends React.Component {
  // note that `navigationOptions` is now a function that receives `navigation`
  static navigationOptions = ({navigation}) => ({
    title: "The Simpsons",
    headerStyle: {
      backgroundColor: "#53b4e6"
    },
    headerTintColor: "#f6c945",
    headerTitleStyle: {
      fontWeight: "bold"
    },
    headerRight: (
      <Button
        onPress={() => navigation.navigate("ImageGallery")}
        title="Gallery"
        color="#f6c945"
      />
    )
  });

[1] https://reactnavigation.org/docs/en/navigation-options.html