Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

utils.ts

111 lines3.1 KB

1import * as React from 'react'
2​
3export function useStableCallback<T extends (...args: Array<any>) => any>(
4 fn: T,
5): T {
6 const fnRef = React.useRef(fn)
7 fnRef.current = fn
8​
9 const ref = React.useRef((...args: Array<any>) => fnRef.current(...args))
10 return ref.current as T
12​
13export const useLayoutEffect =
14 typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
15​
16/**
17 * Taken from https://www.developerway.com/posts/implementing-advanced-use-previous-hook#part3
18 */
19export function usePrevious<T>(value: T): T | null {
20 // initialise the ref with previous and current values
21 const ref = React.useRef<{ value: T; prev: T | null }>({
22 value: value,
23 prev: null,
24 })
25​
26 const current = ref.current.value
27​
28 // if the value passed into hook doesn't match what we store as "current"
29 // move the "current" to the "previous"
30 // and store the passed value as "current"
31 if (value !== current) {
32 ref.current = {
33 value: value,
34 prev: current,
35 }
36 }
37​
38 // return the previous value only
39 return ref.current.prev
41​
42/**
43 * React hook to wrap `IntersectionObserver`.
44 *
45 * This hook will create an `IntersectionObserver` and observe the ref passed to it.
46 *
47 * When the intersection changes, the callback will be called with the `IntersectionObserverEntry`.
48 *
49 * @param ref - The ref to observe
50 * @param intersectionObserverOptions - The options to pass to the IntersectionObserver
51 * @param options - The options to pass to the hook
52 * @param callback - The callback to call when the intersection changes
53 * @returns The IntersectionObserver instance
54 * @example
55 * ```tsx
56 * const MyComponent = () => {
57 * const ref = React.useRef<HTMLDivElement>(null)
58 * useIntersectionObserver(
59 * ref,
60 * (entry) => { doSomething(entry) },
61 * { rootMargin: '10px' },
62 * { disabled: false }
63 * )
64 * return <div ref={ref} />
65 * ```
66 */
67export function useIntersectionObserver<T extends Element>(
68 ref: React.RefObject<T | null>,
69 callback: (entry: IntersectionObserverEntry | undefined) => void,
70 intersectionObserverOptions: IntersectionObserverInit = {},
71 options: { disabled?: boolean } = {},
72) {
73 React.useEffect(() => {
74 if (
75 !ref.current ||
76 options.disabled ||
77 typeof IntersectionObserver !== 'function'
78 ) {
79 return
80 }
81​
82 const observer = new IntersectionObserver(([entry]) => {
83 callback(entry)
84 }, intersectionObserverOptions)
85​
86 observer.observe(ref.current)
87​
88 return () => {
89 observer.disconnect()
90 }
91 }, [callback, intersectionObserverOptions, options.disabled, ref])
93​
94/**
95 * React hook to take a `React.ForwardedRef` and returns a `ref` that can be used on a DOM element.
96 *
97 * @param ref - The forwarded ref
98 * @returns The inner ref returned by `useRef`
99 * @example
100 * ```tsx
101 * const MyComponent = React.forwardRef((props, ref) => {
102 * const innerRef = useForwardedRef(ref)
103 * return <div ref={innerRef} />
104 * })
105 * ```
106 */
107export function useForwardedRef<T>(ref?: React.ForwardedRef<T>) {
108 const innerRef = React.useRef<T>(null)
109 React.useImperativeHandle(ref, () => innerRef.current!, [])
110 return innerRef