MCPcopy Create free account
hub / github.com/TanStack/query / useSyncExternalStoreWithSelector

Function useSyncExternalStoreWithSelector

packages/preact-query/src/utils.ts:65–88  ·  view source on GitHub ↗
(
  subscribe: (onStoreChange: () => void) => () => void,
  getSnapshot: () => TSnapshot,
  selector: (snapshot: TSnapshot) => TSelected,
  isEqual: (a: TSelected, b: TSelected) => boolean,
)

Source from the content-addressed store, hash-verified

63}
64
65export function useSyncExternalStoreWithSelector<TSnapshot, TSelected>(
66 subscribe: (onStoreChange: () => void) => () => void,
67 getSnapshot: () => TSnapshot,
68 selector: (snapshot: TSnapshot) => TSelected,
69 isEqual: (a: TSelected, b: TSelected) => boolean,
70): TSelected {
71 const selectedSnapshotRef = useRef<TSelected | undefined>()
72
73 const getSelectedSnapshot = () => {
74 const snapshot = getSnapshot()
75 const selected = selector(snapshot)
76
77 if (
78 selectedSnapshotRef.current === undefined ||
79 !isEqual(selectedSnapshotRef.current, selected)
80 ) {
81 selectedSnapshotRef.current = selected
82 }
83
84 return selectedSnapshotRef.current
85 }
86
87 return useSyncExternalStore(subscribe, getSelectedSnapshot)
88}

Callers

nothing calls this directly

Calls 1

useSyncExternalStoreFunction · 0.85

Tested by

no test coverage detected