相关疑难解决方法(0)

onClick 事件侦听器在 Remix 中不起作用

在我的 Remix 应用程序中,我尝试根据状态变量的值有条件地显示 UI 小部件。这是我的代码。

import { useState } from "react";
import type { LinksFunction } from "remix";
import stylesUrl from "../styles/index.css";

export const links: LinksFunction = () => {
  return [
    {
      rel: "stylesheet",
      href: stylesUrl
    }
  ];
};

export default function Index() {
  const [isMenuOpen,setMenuOpen] = useState(false)

  function toggleNav(){
    window.alert("hh") // no alert is shown
    console.log("hi") // no console statement is printed
    setMenuOpen(!isMenuOpen)
  }

  return (
    <div className="landing">
     <button onClick={toggleNav}>test</button>
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

但是,toggleNav单击按钮似乎不会触发功能。我在控制台中看不到任何警报或输出。

我不明白为什么它不起作用。如果有人能指出我在这里做错了什么,那就太好了。TIA。

javascript event-listener remix.run

7
推荐指数
1
解决办法
5450
查看次数

标签 统计

event-listener ×1

javascript ×1

remix.run ×1