首页 > 解决方案 > React Native 无法读取未定义的属性“导航”

问题描述

我试图在我的主屏幕上有 2 个可触摸的不透明度,一个用于导航到创建屏幕,一个用于导航到搜索屏幕。我的标签导航中嵌套了堆栈导航,我的主页选项卡会显示主页堆栈等。每当我触摸我的搜索或创建按钮时,它都会弹出一个屏幕,显示“无法读取未定义的属性'导航'”

import React from "react";
import {
  View,
  StyleSheet,
  Text,
  ImageBackground,
  Image,
  Button,
  TouchableOpacity,
} from "react-native";
import { LinearGradient } from "expo-linear-gradient";
import colors from "../config/colors";
import { withNavigation } from "react-navigation";

export default function HomePageScreen({ navigation }) {
  return (
    <ImageBackground
      style={styles.background}
      source={require("../assets/background.jpg")}
    >
      <Image style={styles.logo} source={require("../assets/logo2.png")} />
      {/* the create button */}
      <LinearGradient
        colors={[colors.primary, colors.secondary]}
        start={{ x: 0, y: 0 }}
        end={{ x: 1, y: 0 }}
        style={styles.createButton}
      >
        <TouchableOpacity
          style={styles.touch}
          onPress={() => navigation.navigate("Create Project")}
        >
          <Text style={styles.buttonText}>Create</Text>
        </TouchableOpacity>
      </LinearGradient>
      {/* the search button */}
      <LinearGradient
        colors={[colors.primary, colors.secondary]}
        start={{ x: 0, y: 0 }}
        end={{ x: 1, y: 0 }}
        style={styles.searchButton}
      >
        <TouchableOpacity
          style={styles.touch}
          onPress={() => navigation.navigate("Search Projects")}
        >
          <Text style={styles.buttonText}>Search</Text>
        </TouchableOpacity>
      </LinearGradient>
    </ImageBackground>
  );
}

const styles = StyleSheet.create({
  background: {
    width: "100%",
    height: "100%",
    alignItems: "center",
    justifyContent: "center",
  },

  logo: {
    height: 275,
    width: 275,
    position: "absolute",
    top: 60,
  },

  createButton: {
    height: 70,
    width: 325,
    position: "absolute",
    bottom: 150,
    borderRadius: 999,
    alignItems: "center",
    justifyContent: "center",
  },
  touch: {
    height: 70,
    width: 325,
    borderRadius: 999,
    textAlignVertical: "center",
    justifyContent: "center",
  },

  searchButton: {
    height: 70,
    width: 325,
    position: "absolute",
    bottom: 50,
    borderRadius: 999,
    alignItems: "center",
    justifyContent: "center",
  },

  buttonText: {
    color: "white",
    fontSize: 28,
    fontWeight: "bold",
    alignSelf: "center",
  },
});

标签: javascriptreactjsreact-nativeexporeact-navigation

解决方案


您应该使用 withNavigation 包装您的组件以访问导航

import React from "react";
import {
  View,
  StyleSheet,
  Text,
  ImageBackground,
  Image,
  Button,
  TouchableOpacity,
} from "react-native";
import { LinearGradient } from "expo-linear-gradient";
import colors from "../config/colors";
import { withNavigation } from "react-navigation";

function HomePageScreen({ navigation }) {
  return (
    <ImageBackground
      style={styles.background}
      source={require("../assets/background.jpg")}
    >
      <Image style={styles.logo} source={require("../assets/logo2.png")} />
      {/* the create button */}
      <LinearGradient
        colors={[colors.primary, colors.secondary]}
        start={{ x: 0, y: 0 }}
        end={{ x: 1, y: 0 }}
        style={styles.createButton}
      >
        <TouchableOpacity
          style={styles.touch}
          onPress={() => navigation.navigate("Create Project")}
        >
          <Text style={styles.buttonText}>Create</Text>
        </TouchableOpacity>
      </LinearGradient>
      {/* the search button */}
      <LinearGradient
        colors={[colors.primary, colors.secondary]}
        start={{ x: 0, y: 0 }}
        end={{ x: 1, y: 0 }}
        style={styles.searchButton}
      >
        <TouchableOpacity
          style={styles.touch}
          onPress={() => navigation.navigate("Search Projects")}
        >
          <Text style={styles.buttonText}>Search</Text>
        </TouchableOpacity>
      </LinearGradient>
    </ImageBackground>
  );
}

const styles = StyleSheet.create({
  background: {
    width: "100%",
    height: "100%",
    alignItems: "center",
    justifyContent: "center",
  },

  logo: {
    height: 275,
    width: 275,
    position: "absolute",
    top: 60,
  },

  createButton: {
    height: 70,
    width: 325,
    position: "absolute",
    bottom: 150,
    borderRadius: 999,
    alignItems: "center",
    justifyContent: "center",
  },
  touch: {
    height: 70,
    width: 325,
    borderRadius: 999,
    textAlignVertical: "center",
    justifyContent: "center",
  },

  searchButton: {
    height: 70,
    width: 325,
    position: "absolute",
    bottom: 50,
    borderRadius: 999,
    alignItems: "center",
    justifyContent: "center",
  },

  buttonText: {
    color: "white",
    fontSize: 28,
    fontWeight: "bold",
    alignSelf: "center",
  },
});

export default withNavigation(HomePageScreen)

推荐阅读