【发布时间】:2022-01-15 01:43:23
【问题描述】:
我正在开发一个动态侧边栏,它根据登录的用户映射菜单项。
我遇到的问题是触发功能组件内的功能。我的组件看起来像这样 -
popout.js
import React from 'react'
export default function PopOut({listitems}) {
const togglePopout =()=>{
console.log("Popout")
}
return (
listitems.map((child) => {
<div>{child.label}</div>;
});
)
}
menu.js
import PopOut from "./popout.js"
import React from 'react'
export default function menu() {
const menuitems = [
{
label: "Menu 1",
children: [{ label: "Dashboard" }, { label: "item2" }],
}
{
label: "Menu 2",
children: [{ label: "People" }, { label: "item3" }],
}
{
label: "Menu 3",
children: [{ label: "Payments" }, { label: "item4" }],
}
];
return (
{
// here I want to click the menu and trigger the togglePopout function inside the component.
menuitems.map((item) => {
<div onClick={()=>togglePopout()}>{item.label}</div>;
{
<Popout listItems={item.children}/>
}
});
}
)
}
我想点击菜单并触发组件内部的togglePopout函数
非常感谢任何帮助
【问题讨论】:
标签: reactjs function components