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

问题描述 投票:0回答:2

在我的 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
功能似乎不会在单击按钮时触发。我在控制台中看不到任何警报或输出。

我不明白为什么它不起作用。如果有人能指出我在这里做错了什么,那就太好了。短暂性脑缺血发作。

javascript event-listener remix.run
2个回答
14
投票

确保您在根路由中渲染来自 Remix 的 Scripts 组件,没有它,您的应用程序将不会加载任何 JS 客户端。

参见 https://remix.run/docs/en/v1/api/remix#meta-links-scripts


0
投票

我仍然遇到这个问题,已经检查了根路由中的脚本组件,它就在那里。 你能帮忙吗?请提供更多详细信息,因为我们都是 Shopify 新手

© www.soinside.com 2019 - 2024. All rights reserved.