ReactJs为什么在一个简单组件中会获得两个控制台日志?

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

ReactJs why do i get two console logs in a simple component?

问题

export const NavBar = () => {
  const [showNav, setShowNav] = useState(false);

  const handleNavClick = () => {
    setShowNav(!showNav);
  };

  console.log("hi");

  return (
    <>
      <nav className="flex items-center justify-between pl-8 pr-16 fixed w-full border h-20 top-0 bg-white/30 backdrop-blur-sm z-10">
        {/* Logo */}
        <img
          src="https://res.cloudinary.com/dv8nczwtj/image/upload/v1684896617/Logo_jivlnb.png"
          alt="Logo"
          className="logo"
        />
        {/* Nav WEB*/}
        <div className="md:flex flex-row space-x-5 hidden">
          <a href="#" className="brand">
            Apple
          </a>
          <a href="#" className="brand">
            Samsung
          </a>
          <a href="#" className="brand">
            Xiaomi
          </a>
          <a href="#" className="brand">
            Google
          </a>
        </div>
        {/* BTN Nav Mobil */}
        <button className="md:hidden" onClick={handleNavClick}>
          <img
            src="https://res.cloudinary.com/dv8nczwtj/image/upload/v1684859901/menu_wh8ccz.png"
            alt="Menu"
            className="w-6"
          />
        </button>
        {/* Cart */}
        <CartWidget />
      </nav>
      {/* Nav Mobil */}
      {showNav && (
        <div
          className="flex fixed w-full flex-col justify-center items-center space-y-4 pb-2 border-b-2 border-black md:hidden bg-white/30 top-20 pt-4 backdrop-blur-sm"
          style={{ animation: "fadeIn .5s linear" }}
        >
          <a href="#" className="brand">
            Apple
          </a>
          <a href="#" className="brand">
            Samsung
          </a>
          <a href="#" className="brand">
            Xiaomi
          </a>
          <a href="#" className="brand">
            Google
          </a>
        </div>
      )}
    </>
  );
};
英文:

i have this simple nav component, but it drives me crazy because it makes every console log i make in the app two times.
Im new to React btw.

export const NavBar = () =&gt; {
const [showNav, setShowNav] = useState(false);
const handleNavClick = () =&gt; {
setShowNav(!showNav);
};
console.log(&quot;hi&quot;);
return (
&lt;&gt;
&lt;nav className=&quot;flex items-center justify-between pl-8 pr-16 fixed w-full border h-20 top-0 bg-white/30 backdrop-blur-sm z-10&quot;&gt;
{/* Logo */}
&lt;img
src=&quot;https://res.cloudinary.com/dv8nczwtj/image/upload/v1684896617/Logo_jivlnb.png&quot;
alt=&quot;Logo&quot;
className=&quot;logo&quot;
/&gt;
{/* Nav WEB*/}
&lt;div className=&quot;md:flex flex-row space-x-5 hidden&quot;&gt;
&lt;a href=&quot;#&quot; className=&quot;brand&quot;&gt;
Apple
&lt;/a&gt;
&lt;a href=&quot;#&quot; className=&quot;brand&quot;&gt;
Samsung
&lt;/a&gt;
&lt;a href=&quot;#&quot; className=&quot;brand&quot;&gt;
Xiaomi
&lt;/a&gt;
&lt;a href=&quot;#&quot; className=&quot;brand&quot;&gt;
Google
&lt;/a&gt;
&lt;/div&gt;
{/* BTN Nav Mobil */}
&lt;button className=&quot;md:hidden&quot; onClick={handleNavClick}&gt;
&lt;img
src=&quot;https://res.cloudinary.com/dv8nczwtj/image/upload/v1684859901/menu_wh8ccz.png&quot;
alt=&quot;Menu&quot;
className=&quot;w-6&quot;
/&gt;
&lt;/button&gt;
{/* Cart */}
&lt;CartWidget /&gt;
&lt;/nav&gt;
{/* Nav Mobil */}
{showNav &amp;&amp; (
&lt;div
className=&quot;flex fixed w-full flex-col justify-center items-center space-y-4 pb-2 border-b-2 border-black md:hidden bg-white/30 top-20 pt-4 backdrop-blur-sm&quot;
style={{ animation: &quot;fadeIn .5s linear&quot; }}
&gt;
&lt;a href=&quot;#&quot; className=&quot;brand&quot;&gt;
Apple
&lt;/a&gt;
&lt;a href=&quot;#&quot; className=&quot;brand&quot;&gt;
Samsung
&lt;/a&gt;
&lt;a href=&quot;#&quot; className=&quot;brand&quot;&gt;
Xiaomi
&lt;/a&gt;
&lt;a href=&quot;#&quot; className=&quot;brand&quot;&gt;
Google
&lt;/a&gt;
&lt;/div&gt;
)}
&lt;/&gt;
);
};

double console.log

I have tried putting the handleClick function in a useEffect but then when i put it on the onClick it says that the function is never declared

答案1

得分: 0

你的应用程序是否正在使用 React.StrictModehttps://react.dev/reference/react/StrictMode#fixing-bugs-found-by-double-rendering-in-development)?

英文:

Is your app using React.StrictMode (https://react.dev/reference/react/StrictMode#fixing-bugs-found-by-double-rendering-in-development) ?

huangapple
  • 本文由 发表于 2023年6月1日 23:17:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383411.html
匿名

发表评论

匿名网友

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

确定