0

I wanna fetch the products from my products.json file and display it in my App.js by using map, but it doesn't work. Is there any way i can fix this? Btw thank you for helping me!

App.js:

import "./styles.css";
import { products } from "./data";

export default function App() {
  return (
    <div className="App">
      {products.map((product) => {
        return <div>products</div>;
      })}
    </div>
  );
}

data.json:

{
  products:
   [
    {
      id:1,
      img: "https://vcdn-vnexpress.vnecdn.net/2017/04/27/strawberry-2129-1493287309.jpg",
      title:"strawberry",
      price: 3
    },
    {
      id:2,
      img: "https://cdn.sallysbakingaddiction.com/wp-content/uploads/2012/01/mm-cookies.jpg",
      title:"cookies",
      price: 5
    },
    {
      id:3,
      img: "https://rangsua.vn/wp-content/uploads/2019/05/juice-bia.jpg",
      title:"juice",
      price: 7
    }
  ]
}
    
Sandbox link: https://codesandbox.io/s/cart-vjhfb?file=/src/data.json  
2
  • 2
    It seems to be working fine, try changing your return statement to <div>products - {product.title}</div>; Commented Jan 4, 2022 at 4:14
  • wel i tried and it's like this: codesandbox.io/s/cart-vjhfb?file=/src/App.js Commented Jan 4, 2022 at 4:34

2 Answers 2

1

Two problems. (both likely to be silly mistakes)

  1. You want to use product variable, but you are using products
  2. You need to wrap product with {}. It will look like {product.title}
Sign up to request clarification or add additional context in comments.

Comments

0

You return the <div>products</div>, that's the problem. replace with <div>{product.title}</div>; code

Try this code it's working

import "./styles.css";
import { products } from "./data";

export default function App() {
  return (
    <div className="App">
      {products.map((product) => {
        return <div>{product.title}</div>;
      })}
    </div>
  );
}

2 Comments

well i tried and it's like this: codesandbox.io/s/cart-vjhfb?file=/src/App.js
oky, if my answer help you so accept it and vote up :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.