0

Since I am new to react, I have a function in my parent component like below

const openMenu = (item: Iitem) => {
    setNavOpen(item.id);
    closeMenu();
}

<ul>
    {navItems.map((navItem) => (
        <NavItem
            data={navItem}
            key={navItem.id}
            openMenu={openMenu}
        />
    ))}
</ul>

And I am trying to access the openMenu function from my child component

like this

<ItemButton
    id={data.id}
    onClick={openMegaMenu}
>
    Hello Test
</ItemButton>

I am trying to use the function like onClick={openMegaMenu(data)} but it throws an error like uncaught-typeerror-cannot-read-property-nextsibling-of-undefined . Can you please tell me what is the wrong here.

1
  • Can you show a bit more of your child component? When did the function changed name? What is data? Anyway you should call your function like this: onClick={() => {openMegaMenu(data);}} Commented Apr 25, 2022 at 15:38

1 Answer 1

1

You were close! You want to pass a function to be called, not call the function, so the following should work

onClick={() => openMegaMenu(data)}
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.