UNPKG

810 BJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = useCallbackRef;
5var _react = require("react");
6/**
7 * A convenience hook around `useState` designed to be paired with
8 * the component [callback ref](https://reactjs.org/docs/refs-and-the-dom.html#callback-refs) api.
9 * Callback refs are useful over `useRef()` when you need to respond to the ref being set
10 * instead of lazily accessing it in an effect.
11 *
12 * ```ts
13 * const [element, attachRef] = useCallbackRef<HTMLDivElement>()
14 *
15 * useEffect(() => {
16 * if (!element) return
17 *
18 * const calendar = new FullCalendar.Calendar(element)
19 *
20 * return () => {
21 * calendar.destroy()
22 * }
23 * }, [element])
24 *
25 * return <div ref={attachRef} />
26 * ```
27 *
28 * @category refs
29 */
30function useCallbackRef() {
31 return (0, _react.useState)(null);
32}
\No newline at end of file