-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Experimental] Support promises as fallback data #2891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should avoid upgrading react to a specific version here?
we currently have a spearte testing workflow for react@canary at https://github.com/vercel/swr/actions/workflows/test-canary.yml
so i guess we could create a sperate jest config for this and only test it in react canary ?
@shuding I think this broke something with our types. We modify the type of // We can be confident that data will be nonnull even if the request fails,
// if we defined fallbackData in the config.
data: SWRConfig extends { fallbackData: Response }
? Response
: Response | undefined; This is part of the bigger hook: import { AxiosError } from "axios";
import { useCallback } from "react";
import useSWR, { KeyedMutator, SWRConfiguration } from "swr";
export type Refresh = () => Promise<unknown>;
type GetApi = any;
export const useZmClient = <
Path extends keyof GetApi,
Request extends GetApi[Path]["request"],
Response extends GetApi[Path]["response"],
SWRConfig extends SWRConfiguration<Response>,
>({
path,
pause,
args,
swrConfig,
additionalKey,
}: {
path: Path;
pause?: boolean;
args?: Request;
swrConfig?: SWRConfig;
additionalKey?: string;
}): {
// We can be confident that data will be nonnull even if the request fails,
// if we defined fallbackData in the config.
data: SWRConfig extends { fallbackData: Response }
? Response
: Response | undefined;
error: AxiosError<{ code?: string }> | undefined;
mutate: KeyedMutator<Response>;
refresh: Refresh;
} => {
const swrKey = [path, JSON.stringify(args)];
if (additionalKey) {
swrKey.push(additionalKey);
}
const { data, error, mutate } = useSWR(
pause ? null : swrKey,
() => null as any,
swrConfig,
);
const refresh = useCallback(async () => {
await mutate();
}, [mutate]);
return { data: data!, error, mutate, refresh };
}; This will now give a type error:
I think it would be nice to either:
Or maybe I'm doing something else wrong. If you'd like me to open an issue or try to fix something, please let me know! |
This PR introduces the ability to accept promises as fallback data for SWR read hooks. The behavior is that the resolved value of the promise will be used as fallback instead of the promise itself.
The resolution of the promise is implemented via
use
which means that it will suspend the upper boundary.