Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useCallbackRef.js 720B

123456789101112131415161718192021222324252627
  1. import { useState } from 'react';
  2. /**
  3. * A convenience hook around `useState` designed to be paired with
  4. * the component [callback ref](https://reactjs.org/docs/refs-and-the-dom.html#callback-refs) api.
  5. * Callback refs are useful over `useRef()` when you need to respond to the ref being set
  6. * instead of lazily accessing it in an effect.
  7. *
  8. * ```ts
  9. * const [element, attachRef] = useCallbackRef<HTMLDivElement>()
  10. *
  11. * useEffect(() => {
  12. * if (!element) return
  13. *
  14. * const calendar = new FullCalendar.Calendar(element)
  15. *
  16. * return () => {
  17. * calendar.destroy()
  18. * }
  19. * }, [element])
  20. *
  21. * return <div ref={attachRef} />
  22. * ```
  23. */
  24. export default function useCallbackRef() {
  25. return useState(null);
  26. }