React native 无法在react native中动态设置标题

React native 无法在react native中动态设置标题,react-native,react-navigation,react-navigation-stack,React Native,React Navigation,React Navigation Stack,我试图动态更改新组件屏幕的标题,但出现以下错误: TypeError: TypeError: undefined is not an object (evaluating 'navigationData.navigation.getParam') * screens\CategoryMealsScreen.js:26:42 in navigationOptions * screens\CategoryMealsScreen.js:10:40 in CategoryMealsScreen - no

我试图动态更改新组件屏幕的标题,但出现以下错误:

TypeError: TypeError: undefined is not an object (evaluating 'navigationData.navigation.getParam')
* screens\CategoryMealsScreen.js:26:42 in navigationOptions
* screens\CategoryMealsScreen.js:10:40 in CategoryMealsScreen
- node_modules\react-native\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:9473:27 in renderWithHooks
- node_modules\react-native\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:11994:6 in mountIndeterminateComponent
我的代码:

import React from "react";
import { Text, View, StyleSheet, Button } from "react-native";
import { CATEGORIES } from "../data/dummydata";
import Colors from "../constans/Colors";
let titleHeader = "";
const CategoryMealsScreen = props => {
  const categoryId = props.navigation.getParam("categoryId");

  const selectedCategory = CATEGORIES.find(cat => cat.id === categoryId);
  CategoryMealsScreen.navigationOptions(selectedCategory.title);
  // console.log(Object.keys(props.navigation));
  titleHeader = selectedCategory.title;
  return (
    <View style={styles.screen}>
      <Text>{selectedCategory.title}</Text>

      <Button
        title="Meals Details"
        onPress={() => props.navigation.navigate("MealsDetail")}
      />
    </View>
  );
};

CategoryMealsScreen.navigationOptions = navigationData => {
  const catId = navigationData.navigation.getParam("categoryId");
  const selectedCategory = CATEGORIES.find(cat => cat.id === catId);
  // console.log(catId);
  // console.log(navigationData.navigation.getParam("categoryId"));
  return {
    headerTitle: selectedCategory.title,
    headerStyle: {
      backgroundColor: Colors.primaryColor
    },
    headerTintColor: "white"
  };
};

const styles = StyleSheet.create({
  screen: {
    flex: 1,
    justifyContent: "center",
    alignContent: "center"
  }
});

export default CategoryMealsScreen;
从“React”导入React;
从“react native”导入{文本、视图、样式表、按钮};
从“./data/dummydata”导入{CATEGORIES};
从“./constans/Colors”导入颜色;
让titleHeader=“”;
const CategoryMealsScreen=props=>{
const categoryId=props.navigation.getParam(“categoryId”);
const selectedCategory=CATEGORIES.find(cat=>cat.id==categoryId);
CategoryMalsScreen.navigationOptions(selectedCategory.title);
//console.log(Object.keys(props.navigation));
titleHeader=selectedCategory.title;
返回(
{selectedCategory.title}
props.navigation.navigate(“MealsDetail”)}
/>
);
};
CategoryMalsScreen.navigationOptions=navigationData=>{
const catId=navigationData.navigation.getParam(“categoryId”);
const selectedCategory=CATEGORIES.find(cat=>cat.id==catId);
//控制台日志(catId);
//log(navigationData.navigation.getParam(“categoryId”);
返回{
标题:selectedCategory.title,
头型:{
背景颜色:Colors.primaryColor
},
头部颜色:“白色”
};
};
const styles=StyleSheet.create({
屏幕:{
弹性:1,
辩护内容:“中心”,
内容:“中心”
}
});
导出默认CategoryMalsScreen;
我尝试控制台日志catId,它确实在控制台中显示输出,但错误仍然存在

我可以在组件内部使用
getParam
获取数据,但不能在
CategoryMealsScreen中获取数据。导航选项

一些网站的问题与巴布尔配置,但它不工作,或我做了一些错误的。 不是这样的,我正在使用全局变量
titleHeader
来更改标题标题,它可以工作,但仍然是一个黑客


问题发生的原因是异步任务如查找
类别。查找(cat..这将需要一段时间才能完成

解决方案:使用异步/等待功能,等待任务完成

We can set title dynamically using navigationOptions directly in stack configuration.  

CategoryMeals : {
    screen : CategoryMealsScreen,
    navigationOptions: ({ navigation }) => ({
          title : navigation.getParam('categoryId', 'CategoryMeals')
    }),
},

您正在获取//console.log(navigationData.navigation.getParam(“categoryId”);???是的,但就在那之后,我也收到了该错误。如果您仍然有任何问题,请告诉我您是否正在获取console.log('selectedCategory',selectedCategory);???此显示旧错误未查找的链接不是objectconsole.log('selectedCategory',selectedCategory);向我展示您的日志这里是您可以参考的示例