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 / useFallbackAsInitial.ts

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

24 lines 786 B
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {useRef, useEffect} from 'react';
2
3 // use fallbackData as the initial data on component mount, instead of default data whenever there's a cache miss
4 // adapted from https://viralganatra.com/how-to-fix-swr-to-work%20correctly-with-initialData-or-fallbackData/
5
6 export default function useFallbackAsInitial(useSWRNext) {
7 return (key, fetcher, config) => {
8 const hasMounted = useRef(false);
9
10 useEffect(() => {
11 hasMounted.current = true;
12 }, []);
13
14 // Actual SWR hook.
15 const swr = useSWRNext(key, fetcher, {
16 ...config,
17 fallbackData: hasMounted.current ? undefined : config?.fallbackData,
18 revalidateOnMount: hasMounted.current && config?.fallbackData,
19 });
20
21 return swr;
22 };
23 }
24