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

Function useSyncExternalStoreWithSelector

packages/preact-query/src/utils.ts:86–109  ·  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

84 * @returns The selected part of the current snapshot.
85 */
86export function useSyncExternalStoreWithSelector<TSnapshot, TSelected>(
87 subscribe: (onStoreChange: () => void) => () => void,
88 getSnapshot: () => TSnapshot,
89 selector: (snapshot: TSnapshot) => TSelected,
90 isEqual: (a: TSelected, b: TSelected) => boolean,
91): TSelected {
92 const selectedSnapshotRef = useRef<TSelected | undefined>(undefined)
93
94 const getSelectedSnapshot = () => {
95 const snapshot = getSnapshot()
96 const selected = selector(snapshot)
97
98 if (
99 selectedSnapshotRef.current === undefined ||
100 !isEqual(selectedSnapshotRef.current, selected)
101 ) {
102 selectedSnapshotRef.current = selected
103 }
104
105 return selectedSnapshotRef.current
106 }
107
108 return useSyncExternalStore(subscribe, getSelectedSnapshot)
109}

Callers

nothing calls this directly

Calls 1

useSyncExternalStoreFunction · 0.85

Tested by

no test coverage detected