You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-swr-with-initial-data.ts 451B

1234567891011121314151617181920212223
  1. import { useEffect, useRef } from 'react';
  2. import useSWR from 'swr';
  3. const useSWRWithFallbackData = (
  4. key: string,
  5. fetcher: any,
  6. options = {
  7. fallbackData: {},
  8. }
  9. ) => {
  10. const hasMounted = useRef(false);
  11. useEffect(() => {
  12. hasMounted.current = true;
  13. }, []);
  14. return useSWR(key, fetcher, {
  15. ...options,
  16. fallbackData: hasMounted.current ? undefined : options?.fallbackData,
  17. });
  18. };
  19. export default useSWRWithFallbackData;