MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useDelayedSnapshot

Function useDelayedSnapshot

packages/dev/s2-docs/src/Router.tsx:57–93  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

55}
56
57function useDelayedSnapshot() {
58 let [delayedSnapshot, setDelayedSnapshot] = useState(getSnapshot());
59 let skeletonDisplayTime = useRef(0);
60 useEffect(() => {
61 let timeout;
62 return subscribe(() => {
63 clearTimeout(timeout);
64
65 let snapshot = getSnapshot();
66 if (snapshot.promise) {
67 // Delay skeleton slightly in case network is fast.
68 timeout = setTimeout(() => {
69 setDelayedSnapshot(snapshot);
70 }, SKELETON_DELAY);
71 } else {
72 // Ensure that skeleton shows for a minimum amount of time
73 // if it shows at all, to avoid a jarring flash.
74 let skeletonTimeout = skeletonDisplayTime.current + MIN_SKELETON_TIME - Date.now();
75 if (skeletonTimeout > 0) {
76 timeout = setTimeout(() => {
77 setDelayedSnapshot(snapshot);
78 }, skeletonTimeout);
79 } else {
80 setDelayedSnapshot(snapshot);
81 }
82 }
83 });
84 }, []);
85
86 useEffect(() => {
87 if (delayedSnapshot.promise) {
88 skeletonDisplayTime.current = Date.now();
89 }
90 }, [delayedSnapshot]);
91
92 return delayedSnapshot;
93}
94
95export function NavigationSuspense({children}: {children: React.ReactNode}) {
96 let {isLoading} = useContext(RouterContext)!;

Callers 1

RouterFunction · 0.85

Calls 2

getSnapshotFunction · 0.70
subscribeFunction · 0.70

Tested by

no test coverage detected