javascript - 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",
},
});
解决方案
您应该使用 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)
推荐阅读
- java - 为什么找不到打开的文件,因为文件在那里
- centos7 - 服务器中找不到文件,但已上传
- java - Java - XML 生成 - 空文件
- azure - AcquireTokenAsync:返回什么样的令牌?
- r - ggplot图例(geom_point)中的空白条目?
- google-apps-script - 已部署的编辑器 Google 表格插件未显示给域成员
- mysql - 无法从 Lumen 连接到亚马逊 AWS RDS Mysql
- wolfram-mathematica - Wolfram Mathematica Solve command for a nonlinear system of equations
- kotlin - 在 Kotlin 中通过委托访问委托接收器
- vue.js - 使用函数内联显示渲染时间