[英]Cannot read property 'navigate' of undefined - React Native Navigation
[英]React Native Cannot read property 'navigate' of undefined
我想在我的主屏幕上设置 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",
},
});
我试图在我的主屏幕上设置 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.