PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Views / Components / ListTable / hooks / lagData.ts

lagData.ts in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Views/Components/ListTable/hooks/lagData.ts

38 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {useRef, useEffect, useCallback} from 'react';
2
3 // SWR middleware for keeping the data even if key changes.
4 export default function lagData(useSWRNext) {
5 return (key, fetcher, config) => {
6 // Use a ref to store previous returned data.
7 const laggyDataRef = useRef();
8
9 // Actual SWR hook.
10 const swr = useSWRNext(key, fetcher, config);
11
12 useEffect(() => {
13 // Update ref if data is not undefined.
14 if (swr.data !== undefined) {
15 laggyDataRef.current = swr.data;
16 }
17 }, [swr.data]);
18
19 // Expose a method to clear the laggy data, if any.
20 const resetLaggy = useCallback(() => {
21 laggyDataRef.current = undefined;
22 }, []);
23
24 // Fallback to previous data if the current data is undefined.
25 const dataOrLaggyData = swr.data === undefined ? laggyDataRef.current : swr.data;
26
27 // Is it showing previous data?
28 const isLagging = swr.data === undefined && laggyDataRef.current !== undefined;
29
30 // Also add a `isLagging` field to SWR.
31 return Object.assign({}, swr, {
32 data: dataOrLaggyData,
33 isLagging,
34 resetLaggy,
35 });
36 };
37 }
38