【发布时间】:2021-06-30 00:44:13
【问题描述】:
我需要从另一个组件更改侧边栏组件的状态。
我的代码:
Sidebar.tsx >
const SideBar: React.FC = () => {
const [open, setOpen] = useState<boolean>(false);
function switchSidebar() {
setOpen(!open);
}
const bar = useMemo(() => <Menu open={open} sidebar={true} />, [open])
return (
<>
<SidebarButton />
{ bar }
</>
}
SidebarButton.tsx >
export const SidebarButton:React.FC = () => {
return(
<svg xmlns="http://www.w3.org/2000/svg" onMouseDown={/* ? */}>
<path stroke="currentColor" strokeWidth={3} d="M0 1.5h28M0 10.5h28M8 19.5h20"/>
</svg>
)
}
菜单.tsx >
const Menu: React.FC = ({sidebar, open, data = HeaderData}) => {
return (
<ul>
<SidebarButton />
{ /* Other things to render */ }
</ul>
)
}
您如何看到我需要一种方法来从 SidebarButton 更改 Sidebar 中包含的状态。
【问题讨论】: