简体   繁体   English

解析错误:“,”预期。 具有数组映射功能

[英]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.

相关问题 “解析错误:预期的参数表达式。eslint”与 React 中的 Typescript 映射函数 - "Parsing error: Argument expression expected. eslint" with Typescript map function in React 解析错误:意外的标记,应为“;” .map 函数中的第 54 行 - Parsing error: Unexpected token, expected ";" line 54 in .map function javascript 推送 function 未按预期工作。 我正在尝试将数据推送到数组或列表 - javascript push function is not working as expected. I am trying to push data to array or list 数组中的散布操作出错。 TS1005:“,”应为。 打字稿 - Error with spread operation in array. TS1005: ',' expected. TypeScript 停止 function 未按预期工作。 需要帮忙。 反应 - Stop function is not working as expected. Need Help. React 反应:错误:呈现的钩子比预期的少。 这可能是由 - REACT : Error: Rendered fewer hooks than expected. This may be caused by an 类型错误:解析数组时 LIST.map 不是 function - TypeError: LIST.map is not a function when parsing an array 索引无法在React array.Map函数中按预期工作 - index isn't working as expected in React array.Map function 期望表达。 与埃斯林特和反应 - Expression expected. with eslint and react 解析错误:意外的令牌,预期的“; - Parsing error: Unexpected token, expected ";
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM