599Item.js:12未熟悉的TypeError:product.map不是React的功能

发布于 2025-02-12 20:29:55 字数 2487 浏览 0 评论 0原文

我将我的第一个反应作为练习来理解,但我会发现此错误

599Item.js:12 uck offeraght typeError:product.map在component item.js on Line.js中不是一个函数

。 的产品对象进行的产品对象,

import React, {useState, useEffect} from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css';
import Count from './ItemCount';

export const Item =(({product}) =>  {

  const [sellProduct, setSellProduct] = useState(product);

  useEffect(() => { 
    setSellProduct({product}); 
  }, [product]);

  const mensaje = () => {
    alert ('Gracias por su compra');
  }

return (
    <>
    {
      sellProduct.map((item) => {
        return(
          <Count stock={item.stock} onAdd ={mensaje} >
            <div id= {item.id}>
              <h3>{item.name}</h3> - <small>{item.category}</small>
              <img src= {item.picture} alt="Producto" className="itemImg" />
              <p>{item.description}</p>
              <p>{item.price}</p>
            </div>          
          </Count>
        )
      })
    }
    </>
  )
});

expor default Item;

有一个是我以提取

然后将其传递给子组件,该组件将道具传递给项目组件,我复制两个itemListContainer(parent)

import React, { useEffect, useState } from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css';  
import ItemList from './ItemList';


export const ItemListContainer =() =>  {


  
  const [swSell,setSwSell] = useState([])

  useEffect(() => {
    fetch('https://fakestoreapi.com/products')
      .then((res) => res.json())
      .then((data) => {
        setSwSell(data);
      })
      .catch((err) => {
        console.log(err);
      });
    }, []);

  return(
    <div className="parent">
      {
        swSell.map((item) =>(
          <div className="child" key={item.id}>
            <ItemList item={swSell} />
          </div>
        ))
      }
    </div>
    )}
export default ItemListContainer;

的代码Itemellist(儿童),这将传递给项目组成部分,

import React from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css'; 
import Item from './Item';

export const ItemList = (({item}) =>  {
  return (
    <div className="child">
      {
        item.map((item) =>(
          <Item product= {item} />
        ))
      }
    </div>
  )
});

export default ItemList;

感谢您的帮助

I'm putting together my first react as an exercise to understand but I get this error

599Item.js:12 Uncaught TypeError: product.map is not a function in component item.js on line 12

I copy the component item,js, it has a prop that is the product object

import React, {useState, useEffect} from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css';
import Count from './ItemCount';

export const Item =(({product}) =>  {

  const [sellProduct, setSellProduct] = useState(product);

  useEffect(() => { 
    setSellProduct({product}); 
  }, [product]);

  const mensaje = () => {
    alert ('Gracias por su compra');
  }

return (
    <>
    {
      sellProduct.map((item) => {
        return(
          <Count stock={item.stock} onAdd ={mensaje} >
            <div id= {item.id}>
              <h3>{item.name}</h3> - <small>{item.category}</small>
              <img src= {item.picture} alt="Producto" className="itemImg" />
              <p>{item.description}</p>
              <p>{item.price}</p>
            </div>          
          </Count>
        )
      })
    }
    </>
  )
});

expor default Item;

I take the prop out in a fetch and pass it to the child component, which passes the prop to the items component and I copy the code of both

ItemListContainer(parent)

import React, { useEffect, useState } from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css';  
import ItemList from './ItemList';


export const ItemListContainer =() =>  {


  
  const [swSell,setSwSell] = useState([])

  useEffect(() => {
    fetch('https://fakestoreapi.com/products')
      .then((res) => res.json())
      .then((data) => {
        setSwSell(data);
      })
      .catch((err) => {
        console.log(err);
      });
    }, []);

  return(
    <div className="parent">
      {
        swSell.map((item) =>(
          <div className="child" key={item.id}>
            <ItemList item={swSell} />
          </div>
        ))
      }
    </div>
    )}
export default ItemListContainer;

ItemList(child), this passes to the Item component

import React from "react";
import '../App.css';
import 'materialize-css/dist/css/materialize.css'; 
import Item from './Item';

export const ItemList = (({item}) =>  {
  return (
    <div className="child">
      {
        item.map((item) =>(
          <Item product= {item} />
        ))
      }
    </div>
  )
});

export default ItemList;

I appreciate your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2025-02-19 20:29:55

在项目组件中而不是将

product.map((item)...

其替换为

product && product.map((item)...

In Item component instead of

product.map((item)...

replace it with

product && product.map((item)...
Spring初心 2025-02-19 20:29:55

错误:

product.map不是React

的功能

,因为product您将其设置为sellproduct不是array。它实际上是swsell数组的项目(对象)。

因此,首先,更改以下内容:

const ComponentName = (({ ... }) => {
   ...
})

进入:

const ComponentName = ({ ... }) => {
   ...
}

下一步,因为swsell状态是项目对象的集合,其中项目本身就是产品本身,那么您只需要两个组件: item item list < /strong>作为父母,项目作为渲染项目详细信息的孩子

const Item = ({item}) =>  {
    return (
        <div id= {item.id}>
             <h3>{item.name}</h3> - <small>{item.category}</small>
             <img src={item.image} width="100"/>
             <p>{item.description}</p>
             <p>{item.price}</p>
        </div> 
    )
};

function ItemList() {
    const [swSell,setSwSell] = React.useState([])

    React.useEffect(() => {
        fetch('https://fakestoreapi.com/products')
            .then((res) => res.json())
            .then((data) => {
                setSwSell(data);
            })
            .catch((err) => {
                console.log(err);
            });
    }, []);

    return (
        <div>
            {swSell.map((item) => (
                <div className="child" key={item.id}>
                    <Item item={item} />
                </div>
            ))}
        </div>
    );
}

ReactDOM.render(<ItemList />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

The error:

product.map is not a function in react

arise since the product props that you set as sellProduct is not an array. It is actually an item (object) of swSell array.

So, first, change this:

const ComponentName = (({ ... }) => {
   ...
})

into this:

const ComponentName = ({ ... }) => {
   ...
}

Next, since the swSell state is a collection of item object where the item is the product itself, then you only need two component here: ItemList as parent and Item as the child that render the item details

const Item = ({item}) =>  {
    return (
        <div id= {item.id}>
             <h3>{item.name}</h3> - <small>{item.category}</small>
             <img src={item.image} width="100"/>
             <p>{item.description}</p>
             <p>{item.price}</p>
        </div> 
    )
};

function ItemList() {
    const [swSell,setSwSell] = React.useState([])

    React.useEffect(() => {
        fetch('https://fakestoreapi.com/products')
            .then((res) => res.json())
            .then((data) => {
                setSwSell(data);
            })
            .catch((err) => {
                console.log(err);
            });
    }, []);

    return (
        <div>
            {swSell.map((item) => (
                <div className="child" key={item.id}>
                    <Item item={item} />
                </div>
            ))}
        </div>
    );
}

ReactDOM.render(<ItemList />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文