Skip to content

useUpdateLayoutEffect

Layout-effect variant of useUpdateEffect.

Layout-effect variant of useUpdateEffect.

Import

import { useUpdateLayoutEffect } from '@kamod-ch/hooks'

Live demo

Basic demo

Layout-effect variant of useUpdateEffect.

Value: 0

Layout effect runs after mount: 0

import { useState } from 'preact/hooks'
import { useUpdateLayoutEffect } from '@kamod-ch/hooks'

export default function UseUpdateLayoutEffectDemo() {
  const [value, setValue] = useState(0)
  const [runs, setRuns] = useState(0)

  useUpdateLayoutEffect(() => {
    setRuns((count) => count + 1)
  }, [value])

  return (
    <div>
      <p><strong>Value:</strong> {value}</p>
      <p><strong>Layout effect runs after mount:</strong> {runs}</p>
      <button type="button" onClick={() => setValue((count) => count + 1)}>Increment</button>
    </div>
  )
}

API

TypeScript signature
import { useLayoutEffect } from 'preact/hooks';
declare const _default: typeof import("preact/hooks").useEffect | typeof useLayoutEffect;
export default _default;
/

SSR considerations

This hook is safe to import during SSR. If your effect body touches the DOM, keep that work inside the effect callback.

Browser compatibility

Works in any modern browser supported by Preact.