“React Native错误:未定义对象(在评估’prod.title’时)”

huangapple go评论69阅读模式
英文:

React native error undefined is not an object (evaluating 'prod.title')

问题

以下是您要翻译的内容:

"我在尝试运行这段代码时遇到了这个错误。我已经设置了产品变量。我能知道为什么会出现这个错误吗?



(

)}
keyExtractor={(prod,index) => {
return prod._id
}}
/>

"

英文:

I'm getting this error when I try to run this code. I have set the products variable as well. Can I know why this error occurs?

<View style={styles.prodCont}>
  <ScrollView horizontal={true} style={{ width: "100%" }}>
    <FlatList
      data={products}
      renderItem={({prod}) => (
        <ProductCard 
          cardType="social"
          title={prod.title}
          imageUrl={prod.imageUrl}
          price={prod.price}
          unit={prod.unit}
          overallRating={prod.overallRating}
          likes={prod.likes}
          userID={route.params.userEmail} 
        />
       )}
       keyExtractor={(prod,index) => {
         return prod._id
       }}
     />
   </ScrollView>
 </View>

答案1

得分: 0

The object passed to the renderItem function has the following properties:

renderItem({
  item: ItemT,  // 项目
  index: number,  // 索引
  separators: {
    highlight: () => void,  // 高亮
    unhighlight: () => void,  // 取消高亮
    updateProps: (select: 'leading' | 'trailing', newProps: any) => void,  // 更新属性
  }
}): JSX.Element;

In other words, the product is in the item property. Your current code attempts to destructure a property named prod, which doesn't exist.

FlatList renderItem property docs


Unrelated

IMO the ProductCard usage should look closer to this:

<ProductCard 
  cardType="social"
  product={item}
  userID={route.params.userEmail} 
/>

There's no (good) reason to force passing each product property separately. You could allow optional properties in case you want to override the values in the product itself, though.

英文:

The object passed to the renderItem function has the following properties:

renderItem({
  item: ItemT,
  index: number,
  separators: {
    highlight: () =&gt; void;
    unhighlight: () =&gt; void;
    updateProps: (select: &#39;leading&#39; | &#39;trailing&#39;, newProps: any) =&gt; void;
  }
}): JSX.Element;

In other words, the product is in the item property. Your current code attempts to destructure a property named prod, which doesn't exist.

FlatList renderItem property docs


Unrelated

IMO the ProductCard usage should look closer to this:

&lt;ProductCard 
  cardType=&quot;social&quot;
  product={item}
  userID={route.params.userEmail} 
/&gt;

There's no (good) reason to force passing each product property separately. You could allow optional properties in case you want to override the values in the product itself, though.

答案2

得分: 0

FlatList的renderItem函数传递一个带有item属性的参数表示列表中的一个项目此外我认为不需要在FlatList中使用ScrollView因为FlatList具有horizontal属性请尝试切换到以下代码

<View style={styles.prodCont}>
  <FlatList
    horizontal
    data={products}
    renderItem={({ item }) => (
      <ProductCard 
        cardType="social"  
        title={item.title} 
        imageUrl={item.imageUrl} 
        price={item.price} 
        unit={item.unit} 
        overallRating={item.overallRating} 
        likes={item.likes} 
        userID={route.params.userEmail} 
      />
    )}
    keyExtractor={(prod,index) => prod._id}
  />
</View>
英文:

the function renderItem of FlatList passes a parameter with an item attribute, representing an item in the list, and also I believe you don't need the ScrollView with FlatList, as FlatList has the horizontal attribute, try switching to this code:

&lt;View style={styles.prodCont}&gt;
 &lt;FlatList
 horizontal
 data={products}
 renderItem={({ item }) =&gt; (
    &lt;ProductCard 
      cardType=&quot;social&quot;  
      title={item.title} 
      imageUrl={item.imageUrl} 
      price={item.price} 
      unit={item.unit} 
      overallRating={item.overallRating} 
      likes={item.likes} 
      userID={route.params.userEmail} 
    /&gt;
 )}
 keyExtractor={(prod,index) =&gt; prod._id}
 /&gt;
&lt;/View&gt;

huangapple
  • 本文由 发表于 2023年2月18日 11:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定