When i run this Error: TypeError: navigation.getParam is not a function:

When i run this Error: TypeError: navigation.getParam is not a function:

我使用 Flatlist 在主屏幕上显示一些值列表描述和图像。当我按下 Flatlist 项目传递图像源和 id 时。值传递 getparams 但错误是 TypeError: navigation.getParam is not a [=16= 】 ??。请解决问题并编辑我的代码。我是 React Native 的新手,请解决我的问题。

import React from 'react';
import { StyleSheet, Text, View, FlatList, TouchableOpacity, Image } from 'react-native';
import { Card, Rating } from 'react-native-elements'
import { MaterialCommunityIcons } from '@expo/vector-icons';
import { dishesData } from './DishesData';

const MenuDetailScreen = ({ navigation }) => {

  const dish = dishesData.find(
    (dish) => dish.id === this.navigation.getParam('id')

  );

  return (
    <View style={styles.container}>
      <Card>
        <Card.Title
          style={styles.titleTextStyle}>{dish.title}</Card.Title>
        <Image style={{ height: 300, width: 350 }} source={{ uri: dish.imageSource }} />
      </Card>

      <Card>
        <View style={{ flexDirection: "row" }}>
          <Text style={styles.commentsTextStyle}>Comments</Text>
          <View style={{ flexGrow: 1 }} />
          <TouchableOpacity style={{ paddingBottom: 8 }} >
            <MaterialCommunityIcons name="pencil-circle" size={40} color="blue" />
          </TouchableOpacity>
        </View>
        <FlatList
          scrollEnabled={false}
          data={dish.dishDetails}
          keyExtractor={(item) => item.id.toString()}
          renderItem={({ item }) => {
            return (
              <View>
                <Text style={styles.headerTextStyle}>{item.comment}</Text>
                <Rating
                  readonly
                  imageSize={20}
                  startingValue={item.rating}
                  style={styles.ratingStyle}
                />
                <Text style={styles.spacing}>- {item.author} , {item.created_by}</Text>
              </View>
            );
          }}
        />
      </Card>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingBottom: 10
  },
  commentsTextStyle: {
    fontSize: 20,
    fontWeight: 'bold',
    textAlign: 'left'
  },
  textInputStyle: {
    width: "80%",
    padding: 8,
    marginBottom: 8,
    fontSize: 16
  },
  submitButtonStyle: {
    alignItems: 'center',
    width: "80%",
    borderRadius: 5,
    marginBottom: 8,
    padding: 8,
    fontWeight: 'bold',
    backgroundColor: 'rgba(0, 0, 255, 0.6)',
    color: 'black'
  },
  cancelButtonStyle: {
    alignItems: 'center',
    width: "80%",
    borderRadius: 5,
    marginBottom: 8,
    padding: 8,
    fontWeight: 'bold',
    backgroundColor: 'grey',
    color: 'black'
  },
  ratingStyle: {
    alignItems: 'flex-start',
    paddingBottom: 8
  },
  titleTextStyle: {
    fontSize: 20,
    textAlign: 'left'
  },
  textStyle: {
    paddingBottom: 8
  },
  headerTextStyle: {
    fontSize: 16,
    fontWeight: 'bold',
    paddingTop: 8
  },
  spacing: {
    padding: 8
  },
  ratingStyle: {
    alignItems: 'flex-start',
    paddingTop: 8
  }
});

export default MenuDetailScreen;

Error place

7 | const MenuDetailScreen = ({ navigation }) =>{
   8 | 
   9 |   const dish = dishesData.find(
> 10 |     (dish) => dish.id === this.navigation.getParam('id')
  11 |  
  12 |   );
  13 | 
enter code here

更新 MenuDetailScreen >>


    const dish = dishesData.find(
    (dish) => dish.id === this.navigation.getParam('id')  
     );
  

更好的方法是从 React Navigation useNavigation and useRoute


逐步更改

  • 第 1 步
import { useNavigation, useRoute } from '@react-navigation/native';
  • 第 2 步
const navigation = useNavigation();
const route = useRoute();
  • 第 3 步
const { id } = route.params
  • 第 4 步 - 检查导航时是否传递参数

    示例)

    onPress={() => navigation.navigate('SCREEN_NAME_HERE', {
       PARAM_KEY: PARAM_VALUE
    })}
    

完整示例

import React from 'react';
...
import { useNavigation, useRoute } from '@react-navigation/native';

/*
Navigate to this screen by
navigation.navigate('MenuDetailScreen', {
   id: <ID_VALUE_HERE>
})
*/
const MenuDetailScreen = () => {
  const navigation = useNavigation();
  const route = useRoute();
  const { id } = route.params

  const dish = dishesData.find(
    (dish) => dish.id === id
  );

  return (
    ...
  )
}

Once you get it working, I recommend you to go over Type checking with TypeScript for safely navigating between screens in React.