useWindowEvent
useWindowEvent
is a custom hook that assigns an event listener to window
.
Click count: 0
const [count, setCount] = useState(0)
useWindowEvent("click", () => {
setCount((prev) => prev + 1)
})
return <Text>Click count: {count}</Text>
If you use this code, you need to add
"use client"
to the top of the file.Usage
import { useWindowEvent } from "@yamada-ui/react"
import { useWindowEvent } from "@/components/ui"
import { useWindowEvent } from "@workspaces/ui"
const [count, setCount] = useState(0)
useWindowEvent("click", () => {
setCount((prev) => prev + 1)
})