All files / src utils.ts

100% Statements 5/5
100% Branches 2/2
100% Functions 3/3
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17                7x 52x   52x 3x        
import { useCallback } from 'react';
import { useSWRConfig } from 'swr';
 
/**
 * Returns a function that revalidates every SWR cache entry whose key starts with the given URL.
 * It uses the cache from the nearest `SWRConfig` (the one the framework provides to each
 * component), so it must be called from a component rendered inside that provider.
 */
export const useHandleMutate = () => {
  const { mutate } = useSWRConfig();
 
  return useCallback(
    (url: string) => mutate((key) => typeof key === 'string' && key.startsWith(url), undefined),
    [mutate],
  );
};