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

Function useSyncExternalStoreWithSelector

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

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

Callers

nothing calls this directly

Calls 1

useSyncExternalStoreFunction · 0.85

Tested by

no test coverage detected