0

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! Terminal screenshot of error

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;

2 Answers 2

2

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>
        ))}
    </>
)
Sign up to request clarification or add additional context in comments.

1 Comment

Yep that was it, thank you! I had tried that but I think I was missing a parenthese so it wasn't working at the time but cleaned stuff for the sake of this question and forgot to try fragment. Also doing the closing fragment with nodemon or something was weird in vscode it like kept adding the "</>" forever in my file(it was super weird to see it type itself). I had to stop all my server otherwise it kept adding the closing tag
0

Do not use default export and wrap the output in a 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>
  )
    
  }

Comments

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.