How do i get the recipeName using .map() in React?
Below is my dummy data for the recipe.
const recipe = [
{
category: 'Keto',
name: 'Chicken Bistek',
calories: '500',
description: '',
recipes: [
{
recipeName: 'Chicken',
recipeSize: '3 lbs',
},
{
recipeName: 'Garlic',
recipeSize: '4 cloves',
},
{
recipeName: 'Onion',
recipeSize: '1 whole',
},
{
recipeName: 'Soy Sauce',
recipeSize: '1/4 cup',
},
{
recipeName: 'Coconut Oil',
recipeSize: '1 tbsp',
},
{
recipeName: 'Water',
recipeSize: '1 cup',
},
],
},
];
I’ve tried this code below but it wont display the recipes array. It displays the name, category. But recipe.recipeName and recipe.recipeSize wont display in a tag.
const DietList = (props) => {
return (
<>
{props.diets.map((diet) => (
<div key={diet.name}>
<a>{diet.name}</a>
<a>{diet.category}</a>
{diet.recipes.map((recipe) => (
<a key={recipe.recipeName}>{recipe.recipeSize}</a>
))}
</div>
))}
</>
);
};
2
Answers
Your code is fine, just you are not displaying recipeName
Here, I am making use of your dummy data:
I am not sure why you are using
a
tag. Unless, you are going to link it with a href tag.But I have used the
return
in the map, and it’s working fine. Hope, it will work for you too.