Search code examples
reactjstypeerrorreact-props

TypeError: _SchoolProduct__WEBPACK_IMPORTED_MODULE_2___default.a.map is not a function


I am new at ReactJs and try to complete a task from the youtube channel. I created array "products" in "SchoolProduct.js" then using props I passed the value in Product.js. Now in App.js, I used map function to get data (Maybe I understand something wrong about props or map function)

Here is SchoolProduct.js:

 const products = [
 {
  id: "1",
  name: "pencil",
  price: 1,
  description: "this is pencil"
 },
 {
  id: "2",
  name: "rubber",
  price: 10,
  description: "this is rubber"
 }

]

this is my Product.js

import React from "react"
function Product(props)
{
 return
 (
    <div>
     <h2>{props.product.name}</h2>
     <p>{props.product.price.toLocaleString("en-US", {style: "currency", 
      currency: "USD"})}
     - {props.product.description}</p>
    </div>
  )

}

export default Product

and this my App.js

import React, { Component } from 'react';
import Product from "./Product"
import productsData from "./SchoolProduct"


 function App(){

  const productsComponents = productsData.map(item => <Product product= 
   {item}/>)

  return (
   <div>
    {productsComponents}
   </div>
 )
}

export default App;

The Error is: TypeError: _SchoolProduct__WEBPACK_IMPORTED_MODULE_2___default.a.map is not a function

its shows error in App.js line no 8, which is "const productsComponents"

I know I create a silly mistake, but I am trying to improve it


Solution

  • I have to export my error in default way,

    const products = [
     {
      id: "1",
      name: "pencil",
      price: 1,
      description: "this is pencil"
     },
     {
      id: "2",
      name: "rubber",
      price: 10,
      description: "this is rubber"
     }
    ]
    export default products