HandleClick未被识别为一个prop。

huangapple go评论71阅读模式
英文:

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 &quot;react&quot;;
import { NavLink } from &quot;react-router-dom&quot;;
import { RiCloseLine } from &quot;react-icons/ri&quot;;
import { HiOutlineMenu } from &quot;react-icons/hi&quot;;
import { logo } from &quot;..//assets&quot;;
import { links } from &quot;../assets/constants&quot;;

const NavLinks = ({ handleClick }) =&gt; (
  &lt;div className=&quot;mt-10 &quot;&gt;
    {links.map((item) =&gt; (
      &lt;NavLink
        className=&quot;flex flex-row justify-start items-center my-8 text-sm font-medium text-gray-400 hover:text-cyan-400&quot;
        key={item.key}
        to={item.to}
        onClick={() =&gt; handleClick &amp;&amp; handleClick()}
      &gt;
        &lt;item.icon className=&quot;w-6 h-6 mr-2&quot; /&gt;
        {item.name}
      &lt;/NavLink&gt;
    ))}
  &lt;/div&gt;
);

const Sidebar = () =&gt; {
  const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
  return (
    &lt;&gt;
      &lt;div className=&quot;md:flex hidden flex-col w-[240px] py-10 px-4 bg-[#191624]&quot;&gt;
        &lt;img src={logo} alt=&quot;logo&quot; className=&quot;w-full h-14 object-contain&quot; /&gt;
        &lt;NavLinks /&gt;
      &lt;/div&gt;
      &lt;div className=&quot;absolute md:hidden block top-6 right-3 &quot;&gt;
        {mobileMenuOpen ? (
          &lt;RiCloseLine
            className=&quot;w-6 h-6 text-white mr-2&quot;
            handleClick={() =&gt; setMobileMenuOpen(false)}
          /&gt;
        ) : (
          &lt;HiOutlineMenu
            className=&quot;w-6 h-6 text-white mr-2&quot;
            handleClick={() =&gt; setMobileMenuOpen(true)}
          /&gt;
        )}
      &lt;/div&gt;
      &lt;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 ? &quot;left-0&quot; : &quot;-left-full&quot;}`}
      &gt;
        &lt;img src={logo} alt=&quot;logo&quot; className=&quot;w-full h-14 object-contain&quot; /&gt;
        &lt;NavLinks handleClick={() =&gt; setMobileMenuOpen(false)} /&gt;
      &lt;/div&gt;
    &lt;/&gt;
  );
};

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 ? (
  &lt;RiCloseLine
    className=&quot;w-6 h-6 text-white mr-2&quot;
    onClick={() =&gt; setMobileMenuOpen(false)}
  /&gt;
) : (
  &lt;HiOutlineMenu
    className=&quot;w-6 h-6 text-white mr-2&quot;
    onClick={() =&gt; setMobileMenuOpen(true)}
  /&gt;
)}

huangapple
  • 本文由 发表于 2023年5月26日 09:55:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定