From d5d30d3b9c4c03ff27820c01b54636218e28ef04 Mon Sep 17 00:00:00 2001 From: fullex <0xfullex@gmail.com> Date: Tue, 9 Dec 2025 21:25:15 +0800 Subject: [PATCH] feat: add SWR mutate function for optimistic updates in useQuery - Introduced a new `mutate` function in the `useQuery` hook to support optimistic updates. - Updated the documentation to include examples of using `mutate` for immediate cache updates and revalidation. - Enhanced type definitions to ensure compatibility with SWR's `KeyedMutator` for better type safety. --- src/renderer/src/data/hooks/useDataApi.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/data/hooks/useDataApi.ts b/src/renderer/src/data/hooks/useDataApi.ts index e13b0c37c5..de30473ab0 100644 --- a/src/renderer/src/data/hooks/useDataApi.ts +++ b/src/renderer/src/data/hooks/useDataApi.ts @@ -2,6 +2,7 @@ import type { BodyForPath, QueryParamsForPath, ResponseForPath } from '@shared/d import type { ConcreteApiPaths } from '@shared/data/api/apiSchemas' import type { PaginatedResponse } from '@shared/data/api/apiTypes' import { useState } from 'react' +import type { KeyedMutator } from 'swr' import useSWR, { useSWRConfig } from 'swr' import useSWRMutation from 'swr/mutation' @@ -118,6 +119,18 @@ function getFetcher([path, query]: [TPath, Recor * revalidateOnFocus: true * } * }) + * + * // Optimistic updates with mutate (bound to current key, no key needed) + * const { data, mutate } = useQuery(`/topics/${id}`) + * + * // Update cache immediately without revalidation + * await mutate({ ...data, title: 'New Title' }, false) + * + * // Update cache with function and revalidate + * await mutate(prev => ({ ...prev, count: prev.count + 1 })) + * + * // Just revalidate (refetch from server) + * await mutate() * ``` */ export function useQuery( @@ -139,6 +152,8 @@ export function useQuery( error?: Error /** Function to manually refetch data */ refetch: () => void + /** SWR mutate function for optimistic updates */ + mutate: KeyedMutator> } { // Internal type conversion for SWR compatibility const key = options?.enabled !== false ? buildSWRKey(path, options?.query as Record) : null @@ -160,7 +175,8 @@ export function useQuery( data, loading: isLoading || isValidating, error: error as Error | undefined, - refetch + refetch, + mutate } }