这是我的代码:
import React from "react";
import { View, Text, Button, StyleSheet, Platform } from "react-native";
import { CATEGORIES } from "../data/dummy-data";
import Color from "../constants/Color";
,这些都是我的导入,
const CategoryMealScreen = (props) => {
,这是的主要功能
const catId = props.navigation.getParam("categoryId");
const selectedCategory = CATEGORIES.find((cat) => cat.id === catId);
选定类别是文本中使用的常量。
return (
<View style={styles.screen}>
<Text>The Category Meal Screen!</Text>
<Text>{selectedCategory.title}</Text>
<Button
title="Go to Details"
onPress={() => {
props.navigation.navigate({
routeName: "MealDetail",
});
}}
/>
<Button
title="Go Back"
onPress={() => {
props.navigation.pop();
}}
/>
</View>
);
};
CategoryMealScreen.navigationOptions = (navigationData) => {
const catId = navigationData.navigation.getParam("categoryId");
const selectedCategory = CATEGORIES.find((cat) => cat.id === catId);
return {
headerTitle: selectedCategory.title,
headerStyle: {
backgroundColor: Platform.OS === "android" ? Color.primaryColor : "",
},
headerTintColor: Platform.OS === "android" ? "white" : Color.primaryColor,
};
};
这是我正在使用的样式表
const styles = StyleSheet.create({
screen: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
});
export default CategoryMealScreen;
,我想问题可能是来自变量选择的类别,但我不知道如何解决它,,请帮助
发布于 2022-07-09 17:47:06
错误告诉您,您的selectedCategory
变量中没有对象。最明显的原因是您的find
没有找到匹配的。
如果您从虚拟数据中console.log您的CATEGORIES
,从params中获取您的catId
,您应该能够看到出了什么问题。可能是缺少catId,还是没有一个类别具有该ID。
https://stackoverflow.com/questions/72921525
复制相似问题