在我的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。