UNPKG

741 BJavaScriptView Raw
1import { 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 * @category refs
25 */
26
27export default function useCallbackRef() {
28 return useState(null);
29}
\No newline at end of file