global mutate hook

This commit is contained in:
Josh Hawkins 2024-09-24 07:20:27 -05:00
parent cce66e8d9b
commit 0bd70e7695

View File

@ -0,0 +1,16 @@
// https://github.com/vercel/swr/issues/1670#issuecomment-1844114401
import { useCallback } from "react";
import { cache, mutate } from "swr/_internal";
const useGlobalMutation = () => {
return useCallback((swrKey: string | ((key: string) => boolean), ...args) => {
if (typeof swrKey === "function") {
const keys = Array.from(cache.keys()).filter(swrKey);
keys.forEach((key) => mutate(key, ...args));
} else {
mutate(swrKey, ...args);
}
}, []) as typeof mutate;
};
export default useGlobalMutation;