英文:
HandleClick not being recognized as a prop
问题
我尝试让菜单在单击侧边栏时显示,但我遇到了一个错误:“react-dom.development.js:86 警告:React无法识别DOM上的handleClick
属性”
import { useState } from "react";
import { NavLink } from "react-router-dom";
import { RiCloseLine } from "react-icons/ri";
import { HiOutlineMenu } from "react-icons/hi";
import { logo } from "..//assets";
import { links } from "../assets/constants";
const NavLinks = ({ handleClick }) => (
<div className="mt-10 ">
{links.map((item) => (
<NavLink
className="flex flex-row justify-start items-center my-8 text-sm font-medium text-gray-400 hover:text-cyan-400"
key={item.key}
to={item.to}
onClick={() => handleClick && handleClick()}
>
<item.icon className="w-6 h-6 mr-2" />
{item.name}
</NavLink>
))}
</div>
);
const Sidebar = () => {
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
return (
<>
<div className="md:flex hidden flex-col w-[240px] py-10 px-4 bg-[#191624]">
<img src={logo} alt="logo" className="w-full h-14 object-contain" />
<NavLinks />
</div>
<div className="absolute md:hidden block top-6 right-3 ">
{mobileMenuOpen ? (
<RiCloseLine
className="w-6 h-6 text-white mr-2"
handleClick={() => setMobileMenuOpen(false)}
/>
) : (
<HiOutlineMenu
className="w-6 h-6 text-white mr-2"
handleClick={() => setMobileMenuOpen(true)}
/>
)}
</div>
<div
className={`absolute top-0 h-screen w-2/3 bg-gradient-to-tl from-white/10 to-[#483d8b] backdrop-blur-lg z-10 p-6 md:hidden smooth-transition
${mobileMenuOpen ? "left-0" : "-left-full"}`}
>
<img src={logo} alt="logo" className="w-full h-14 object-contain" />
<NavLinks handleClick={() => setMobileMenuOpen(false)} />
</div>
</>
);
};
export default Sidebar;
我确定我遗漏了一个语法问题,但是重做了三次后我找不到它。
英文:
I'm trying to get my menu to show when I click the sidebar, but I'm getting an error "react-dom.development.js:86 Warning: React does not recognize the handleClick
prop on a DOM "
import { useState } from "react";
import { NavLink } from "react-router-dom";
import { RiCloseLine } from "react-icons/ri";
import { HiOutlineMenu } from "react-icons/hi";
import { logo } from "..//assets";
import { links } from "../assets/constants";
const NavLinks = ({ handleClick }) => (
<div className="mt-10 ">
{links.map((item) => (
<NavLink
className="flex flex-row justify-start items-center my-8 text-sm font-medium text-gray-400 hover:text-cyan-400"
key={item.key}
to={item.to}
onClick={() => handleClick && handleClick()}
>
<item.icon className="w-6 h-6 mr-2" />
{item.name}
</NavLink>
))}
</div>
);
const Sidebar = () => {
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
return (
<>
<div className="md:flex hidden flex-col w-[240px] py-10 px-4 bg-[#191624]">
<img src={logo} alt="logo" className="w-full h-14 object-contain" />
<NavLinks />
</div>
<div className="absolute md:hidden block top-6 right-3 ">
{mobileMenuOpen ? (
<RiCloseLine
className="w-6 h-6 text-white mr-2"
handleClick={() => setMobileMenuOpen(false)}
/>
) : (
<HiOutlineMenu
className="w-6 h-6 text-white mr-2"
handleClick={() => setMobileMenuOpen(true)}
/>
)}
</div>
<div
className={`absolute top-0 h-screen w-2/3 bg-gradient-to-tl from-white/10 to-[#483d8b] backdrop-blur-lg z-10 p-6 md:hidden smooth-transition
${mobileMenuOpen ? "left-0" : "-left-full"}`}
>
<img src={logo} alt="logo" className="w-full h-14 object-contain" />
<NavLinks handleClick={() => setMobileMenuOpen(false)} />
</div>
</>
);
};
export default Sidebar;
I'm sure I'm missing a syntax issue, but I after redoing it 3 times I can't find it.
答案1
得分: 1
完整的警告信息如下:
警告:React 不识别 DOM 元素上的
handleClick
属性。如果您有意让它在 DOM 中显示为自定义属性,请将其拼写为小写的handleclick
。如果您是从父组件意外传递的,请从 DOM 元素中删除它。
在 svg
在 IconBase
在 HiOutlineMenu
在 div
在 Sidebar (https://gvq8pm.csb.app/src/App.js:42:67)
在 div
在 App
在 Router (https://gvq8pm.csb.app/node_modules/react-router/dist/index.js:911:18)
在 BrowserRouter (https://gvq8pm.csb.app/node_modules/react-router-dom/dist/index.js:627:18)
react-icon
图标组件将 handleClick
属性泄漏到 DOM 中,由于它是一个非标准的属性,因此会生成警告。请改用 onClick
属性,我认为您本来就应该使用它,因为 handleClick
不会切换任何东西。
英文:
The complete warning is:
> Warning: React does not recognize the handleClick
prop on a DOM
> element. If you intentionally want it to appear in the DOM as a custom
> attribute, spell it as lowercase handleclick
instead. If you
> accidentally passed it from a parent component, remove it from the DOM
> element.
> at svg
> at IconBase
> at HiOutlineMenu
> at div
> at Sidebar (https://gvq8pm.csb.app/src/App.js:42:67)
> at div
> at App
> at Router (https://gvq8pm.csb.app/node_modules/react-router/dist/index.js:911:18)
> at BrowserRouter (https://gvq8pm.csb.app/node_modules/react-router-dom/dist/index.js:627:18)
The react-icon
icon components are leaking the handleClick
prop out to the DOM and since it's a non-standard prop/attribute it generates the warning. Use the onClick
prop instead, which I think you likely meant to use anyway since handleClick
doesn't toggle anything.
{mobileMenuOpen ? (
<RiCloseLine
className="w-6 h-6 text-white mr-2"
onClick={() => setMobileMenuOpen(false)}
/>
) : (
<HiOutlineMenu
className="w-6 h-6 text-white mr-2"
onClick={() => setMobileMenuOpen(true)}
/>
)}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论