useUpdateBreakpointEffect
useUpdateBreakpointEffect
is a custom hook that skips the side effect on the initial render and executes a specific callback function when the breakpoint changes.
The current device is "unknown"
const [device, setDevice] = useState("unknown");
useUpdateBreakpointEffect(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 { useUpdateBreakpointEffect } from "@yamada-ui/react"
import { useUpdateBreakpointEffect } from "@/components/ui"
import { useUpdateBreakpointEffect } from "@workspaces/ui"
const [device, setDevice] = useState("unknown")
useUpdateBreakpointEffect((breakpoint) => {
if (breakpoint === "sm") {
setDevice("mobile")
} else if (breakpoint === "md") {
setDevice("tablet")
} else {
setDevice("desktop")
}
}, [])