Open Pet3r1512 opened 1 year ago
+1 similar here, my component
import React, { PropsWithChildren } from 'react';
import { Menu, MenuList, MenuHandler } from '@material-tailwind/react';
export const FilterDropDown: React.FC<
PropsWithChildren<{
root?: React.ReactNode;
}>
> = ({ children, root }) => {
return (
<Menu placement="bottom">
<MenuHandler>
<div>{root}</div>
</MenuHandler>
<MenuList>{children}</MenuList>
</Menu>
);
};
The answer seems to be is to accept a ref on your child component...
import React from 'react';
interface ChildComponentProps {
// Add any other props here
}
const ChildComponent = React.forwardRef<HTMLDivElement, ChildComponentProps>((props, ref) => {
return <div ref={ref} {...props}>Hello</div>;
});
export default ChildComponent;
@Pet3r1512 can you send me your code there so that I can see because I created one with navbar and it works just fine:
import React from "react";
import {
Navbar,
MobileNav,
Typography,
Button,
IconButton,
Menu,
MenuHandler,
MenuList,
MenuItem,
} from "@material-tailwind/react";
export default function NavbarDefault() {
const [openNav, setOpenNav] = React.useState(false);
React.useEffect(() => {
window.addEventListener(
"resize",
() => window.innerWidth >= 960 && setOpenNav(false),
);
}, []);
const navList = (
<ul className="mb-4 mt-2 flex flex-col gap-2 lg:mb-0 lg:mt-0 lg:flex-row lg:items-center lg:gap-6">
<Menu>
<MenuHandler>
<Typography
as="li"
variant="small"
color="blue-gray"
className="p-1 font-normal"
>
<a href="#" className="flex items-center">
Pages
</a>
</Typography>
</MenuHandler>
<MenuList>
<MenuItem>Menu Item 1</MenuItem>
<MenuItem>Menu Item 2</MenuItem>
<MenuItem>Menu Item 3</MenuItem>
</MenuList>
</Menu>
<Typography
as="li"
variant="small"
color="blue-gray"
className="p-1 font-normal"
>
<a href="#" className="flex items-center">
Account
</a>
</Typography>
<Typography
as="li"
variant="small"
color="blue-gray"
className="p-1 font-normal"
>
<a href="#" className="flex items-center">
Blocks
</a>
</Typography>
<Typography
as="li"
variant="small"
color="blue-gray"
className="p-1 font-normal"
>
<a href="#" className="flex items-center">
Docs
</a>
</Typography>
</ul>
);
return (
<Navbar className="mx-auto max-w-screen-xl px-4 py-2 lg:px-8 lg:py-4">
<div className="container mx-auto flex items-center justify-between text-blue-gray-900">
<Typography
as="a"
href="#"
className="mr-4 cursor-pointer py-1.5 font-medium"
>
Material Tailwind
</Typography>
<div className="hidden lg:block">{navList}</div>
<Button variant="gradient" size="sm" className="hidden lg:inline-block">
<span>Buy Now</span>
</Button>
<IconButton
variant="text"
className="ml-auto h-6 w-6 text-inherit hover:bg-transparent focus:bg-transparent active:bg-transparent lg:hidden"
ripple={false}
onClick={() => setOpenNav(!openNav)}
>
{openNav ? (
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
className="h-6 w-6"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-6 w-6"
fill="none"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M4 6h16M4 12h16M4 18h16"
/>
</svg>
)}
</IconButton>
</div>
<MobileNav open={openNav}>
<div className="container mx-auto">
{navList}
<Button variant="gradient" size="sm" fullWidth className="mb-2">
<span>Buy Now</span>
</Button>
</div>
</MobileNav>
</Navbar>
);
}
I am using Menu component for my navbar and inside Menu, there is a MenuHandler which cause a Error like this:
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?