hi I’m trying to access an array of object like this ( {product.name} ) and get this type of error in reactjs
TypeError: Cannot read property ‘name’ of undefined
here’s the code
import React from 'react';
import data from '../data';
import { Link } from 'react-router-dom';
function ProductScreen(props) {
console.log(props.match.params.id);
const product = data.products.find(x => x._id === props.match.params.id);
return <div>
<div>
<Link to="/">Back to results</Link>
</div>
<h1>{product.name}</h1>
</div>
}
export default ProductScreen;
and this is the array i’m trying to access
export default {
products:[{
_id: 1,
name: '4 season Pizza',
category: 'pizza',
image: '/images/4.jpeg',
price: '7000 rwf',
brand: 'Pizza',
rating: 4.5,
numReviews: 10
},
{
_id: 2,
name: 'ham sandwich',
category: 'sandwitch',
image: '/images/3.jpeg',
price: '1500 rwf',
brand: 'sandwitgh',
rating: 4.5,
numReviews: 10
},
{
_id: 3,
name: 'beef Burger',
category: 'burger',
image: '/images/1.jpeg',
price: '3500 rwf',
brand: 'Burger',
rating: 4.5,
numReviews: 10
},
{
_id: 4,
name: 'chicken Burger',
category: 'burger',
image: '/images/2.jpeg',
price: '4000 rwf',
brand: 'Burger',
rating: 4.5,
numReviews: 10
},
{
_id: 5,
name: 'Veg Burger',
category: 'burger',
image: '/images/1.jpeg',
price: '3000 rwf',
brand: 'Burger',
rating: 4.5,
numReviews: 10
}]
}
help me please I’m trying to learn reactjs