Next.js template
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.js 376B

123456789101112131415
  1. import { useEffect, useRef } from 'react';
  2. import useSWR from 'swr';
  3. export default function useSWRWithFallbackData(key, fetcher, options = {}) {
  4. const hasMounted = useRef(false);
  5. useEffect(() => {
  6. hasMounted.current = true;
  7. }, []);
  8. return useSWR(key, fetcher, {
  9. ...options,
  10. fallbackData: hasMounted.current ? undefined : options?.fallbackData,
  11. });
  12. }