开发者问题收集

无法读取未定义的 React Native 属性“params”

2023-01-18
1542

我正在编写产品详细信息,但无法使用 params 属性

无法读取未定义的属性“params” 您可以在此处查看我的代码

import React from 'react';
import {Image, Text, View} from 'react-native';

const ProductDetail = ({route, navigation}) => {
  const productID = route.params;
  return (
    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      <Text>Product Detail</Text>
    </View>
  );
};

export default ProductDetail;

产品项目



const CardHeight = 220;
const ProductItem = ({list}) => {
  const navigation = useNavigation();
  return (
    <ScrollView>
      {list?.map((item, index) => {
        return (
          
                <TouchableOpacity
                  style={{
                    borderRadius: sizes.radius,
                    overflow: 'hidden',
                    flexDirection: 'row',
                  }}
                  onPress={() => {
                    navigation.navigate('ProductDetails'), {productID: index};
                  }}>
                  .....
  );
};

export default ProductItem;

MainNavigator



const Stack = createStackNavigator();

const MainNavigator = () => {
  return (
    <NavigationContainer>
      <StatusBar hidden />
      <Stack.Navigator>
        
        
        <Stack.Screen
          name="Root"
          component={TabDrawer}
          options={{
            headerShown: false,
            useNativeDriver: true,
            gestureEnabled: false,
          }}
        />
        
        <Stack.Screen
          name="ProductDetails"
          component={productDetailScreen}
          options={{headerShown: false}}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

export default MainNavigator;

我尝试按照将参数传递给反应导航的路线,但没有成功 productDetail

我不知道这个错误是从哪里来的,有人能具体解释给我吗

1个回答

用以下代码替换此 navigation.navigate('ProductDetails'), {productID: index};

navigation.navigate('ProductDetails',{productID: index})
Jatin Bhuva
2023-01-18