0

Im trying to render the following information into a component:

 const navigation = [
  {
    name: "Services",
    description: "Our services",
    href: "#",
    subMenu: [
      { name: "Management" },
      { name: "Development" },
      { name: "Specialists" },
    ],
  },
  {
    name: "Experts",
    description: "our experts",
    href: "#",
    subMenu: [
      { name: "Engineers" },
      { name: "Project managers" },
      { name: "Developers" },
      { name: "Cloud solutions" },
    ],
  }, 

So I tried some different approaches but they won't work

first approach:

{navigation.map((item) => (
        <>
          <div className=" border-b-2 justify-between pb-4 flex ">
            <h3 className="underline-offset-2 ">
              {item.description}
            </h3>
            <div className="border border-solid border-b-2"></div>
            <ChevronDownIcon
              onClick={() => setShowSubMenu(!ShowSubMenu)}
              className="h-5 w-5 "
            />
          </div>

          <div>{item.subMenu}</div>
        </>
      ))} 

Gives error: "Objects are not valid as a React child", but all guides I find says that this should be solved by using .map() which I am but it still gives this error. So I tried using .map() again in the component:

{navigation.map((menu) => (
                <div key={menu.name}>{menu.subMenu}</div>
              ))}

But now it gives this error: "error: objects are not valid as a react child ".

Thanks for your help.

3
  • 1
    Show your entire component please. I'm not sure but did you wrap your {navigation.map()} with React fragment or something ? (<>{navigation.map()}</>) Commented Jul 19, 2022 at 8:43
  • subMenu is an array of objects, you are going to need to map that onto some components/markup as well. Commented Jul 19, 2022 at 8:46
  • this causes error item.subMenu Commented Jul 19, 2022 at 8:46

1 Answer 1

1

You should map through the subMenu because it on it's own is an array. Something along the lines of:

navigation.map((menu) => (
  menu.subMenu.map(sm => {
     // do as you need here
  })
)
Sign up to request clarification or add additional context in comments.

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.