DEMO

useMountSync

Run a function synchronously when a component is mounted and after DOM is painted. Useful when accessing the DOM or creating event listeners.

Usage

import { useMountSync } from '@react-hooks-library/core'

function Demo() {
  useMountSync(() => {
    const el = document.querySelector('#mount-sync')
    alert(`I have been mounted. I am a <${el?.tagName}> tag`)
  })

  return <strong id="mount-sync"></strong>
}

Type Declarations

/**
 * Run a function synchronously when a component is mounted and after DOM is painted.
 *
 * @param callback function to be executed
 */
declare function useMountSync(callback: Fn): void;

Source

Source | Demo | Docs