I am trying to import data from a json file and render a list of images. But I get an error saying: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
This is the file which seems to generate the error:
import React from 'react';
import Product from "./Product/index";
const ProductList = () => {
const renderedList = import("../../../data/data.json").then(json
=> json.goods.map(image => {
return <div><Product images={image.pictures} /></div>
}
));
return <div>{renderedList}</div>
}
export default ProductList;
This is my data.json file:
{
"goods": [
{
"id": "1",
"name": "Cat Tee Black T-Shirt",
"prices": "$ 10.90",
"pictures": "120642730401995392_1.jpg",
"size": "",
"quantity": ""
},
{
"id": "2",
"name": "Dark Thug Blue-Navy T-Shirt",
"prices": "$ 29.45",
"pictures": "51498472915966370_1.jpg",
"size": "",
"quantity": ""
}]
}
This is my Product component:
import React, { Component } from "react";
import Thumb from "../../../Thumb/index";
const Product = props => {
return (
<div className="shelf-item">
<div className="shelf-stopper">Free shipping</div>
<Thumb
classes="shelf-item__thumb"
src={props.images}
/>
<p className="shelf-item__title">product</p>
<div className="shelf-item__price">
productInstallment
</div>
<div className="shelf-item__buy-btn">Add to cart</div>
</div>
);
}
export default Product;
Could someone help me figure it out? Thanks a lot!