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

6
在我的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>
  );
}

然而,点击按钮时似乎没有触发toggleNav函数。 我在控制台中看不到任何警报或输出。

我无法理解为什么它不起作用。 如果有人能指出我在这里做错了什么,那将是非常好的。 TIA。

1个回答

13

在渲染Scripts组件(remix 1.8.1)后,我仍然遇到这个问题。 - Andrew
最新链接:https://remix.run/docs/zh/1.19.3/components/scripts - JP Lew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接