useBreakpointEffect
useBreakpointEffect
is a custom hook that executes a specific callback function when the breakpoint changes.
The current device is "mobile"
const [device, setDevice] = useState("mobile");
useBreakpointEffect(breakpoint => {
if (breakpoint === "sm") {
setDevice("mobile");
} else if (breakpoint === "md") {
setDevice("tablet");
} else {
setDevice("desktop");
}
}, []);
return <Text>The current device is "{device}"</Box>;
If you use this code, you need to add
"use client"
to the top of the file.Usage
import { useBreakpointEffect } from "@yamada-ui/react"
import { useBreakpointEffect } from "@/components/ui"
import { useBreakpointEffect } from "@workspaces/ui"
const [device, setDevice] = useState("mobile")
useBreakpointEffect((breakpoint) => {
if (breakpoint === "sm") {
setDevice("mobile")
} else if (breakpoint === "md") {
setDevice("tablet")
} else {
setDevice("desktop")
}
}, [])