useIsomorphicLayoutEffect
A custom hook that provides a cross-platform compatible useLayoutEffect.
Installation
pnpm dlx shadcn@latest add @reactusekit/use-isomorphic-layout-effect
Usage
import { useIsomorphicLayoutEffect } from "@/hooks/use-isomorphic-layout-effect"
import { useState, useRef } from "react"
function App() {
const [dimensions, setDimensions] = useState({ width: 0, height: 0 })
const elementRef = useRef<HTMLDivElement>(null)
useIsomorphicLayoutEffect(() => {
if (elementRef.current) {
const { offsetWidth, offsetHeight } = elementRef.current
setDimensions({ width: offsetWidth, height: offsetHeight })
}
}, [])
return (
<div>
<div
ref={elementRef}
style={{ padding: "20px", border: "1px solid #ccc" }}
>
This element is being measured
</div>
<p>
Dimensions: {dimensions.width} x {dimensions.height}
</p>
</div>
)
}
API
Parameters
Parameter | Type | Description |
---|---|---|
effect | () => void | (() => void) | The effect function to run |
deps | React.DependencyList | undefined | The dependencies array for the effect |
Returns
This hook doesn't return anything.