[英]Parsing error: ',' expected. with array map function
this is very simple react code, but I cannot figure out why it's so mad at me for trying to do a map.这是非常简单的反应代码,但我不明白为什么它对我试图做一张地图如此生气。 I double check the closing brackets and that the key matches and I've tried adding semi colon all over and nothing happened, the error is "Parsing error: ',' expected."
我仔细检查了右括号和键是否匹配,我尝试在所有地方添加分号但没有任何反应,错误是“解析错误:','预期。” on my recipes object but everything checks out.
在我的食谱对象上,但一切都检查过了。 Thanks for any help!
谢谢你的帮助!
import React from "react";
const RecipeCard = () => {
const recipes = [
{
id: "recipe-id-1",
name: "Tomato Soup",
description: "Love red soup 😎",
ingredient: [
{ name: "tomato", measurement: 3, unit: "cup" },
{ name: "water", measurement: 3, unit: "cup" },
]
},
{
id: "recipe-id-2",
name: "Potato Soup",
description: "Love potato soup 😎",
ingredient: [
{ name: "potato", measurement: 3, unit: "cup" },
{ name: "water", measurement: 3, unit: "cup" },
]
}
];
return (
{recipes.map((recipe) => (
<div className="recipeCard">
<h2>{recipe.name}</h2>
<p>{recipe.description}</p>
<ul>
{recipe.ingredient.map((ingredient) => (
<li>
{ingredient.name} - {ingredient.measurement}
{ingredient.unit}
</li>
))}
</ul>
</div>
))}
)
}
export default RecipeCard;
you need to group the content.您需要对内容进行分组。 use
</>
fragment使用
</>
片段
return (
<>
{recipes.map((recipe) => (
<div className="recipeCard">
<h2>{recipe.name}</h2>
<p>{recipe.description}</p>
<ul>
{recipe.ingredient.map((ingredient) => (
<li>
{ingredient.name} - {ingredient.measurement}
{ingredient.unit}
</li>
))}
</ul>
</div>
))}
</>
)
Do not use default export and wrap the output in a div:不要使用默认导出并将输出包装在 div 中:
import React from "react";
export const RecipeCard = () => {
const recipes = [
{
id: "recipe-id-1",
name: "Tomato Soup",
description: "Love red soup 😎",
ingredient: [
{ name: "tomato", measurement: 3, unit: "cup" },
{ name: "water", measurement: 3, unit: "cup" },
]
},
{
id: "recipe-id-2",
name: "Potato Soup",
description: "Love potato soup 😎",
ingredient: [
{ name: "potato", measurement: 3, unit: "cup" },
{ name: "water", measurement: 3, unit: "cup" },
]
}
];
return (
<div>
{recipes.map((recipe) => (
<div className="recipeCard">
<h2>{recipe.name}</h2>
<p>{recipe.description}</p>
<ul>
{recipe.ingredient.map((ingredient) => (
<li>
{ingredient.name} - {ingredient.measurement}
{ingredient.unit}
</li>
))}
</ul>
</div>
))}
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.