Skip to content

Support React 19 with options: {swr: true} #4

Description

@dancerphil

Is your feature request related to a problem? Please describe.
Fetch data in Components without any custom hooks.

Describe the solution you'd like

const apiGetUser = createInterface('GET', '/user/{id}', {swr: true});

const Component = async ({id}) => {
    const user = await apiGetUser({id});
    return <>{user.name}</>;
}
// or in use hook
const Component = ({id}) => {
    const user = use(apiGetUser({id}));
    return <>{user.name}</>;
}
// And the above code will first fallback to Suspense then renders.

Describe alternatives you've considered
An external utils to wrap api:

const Component = async ({id}) => {
    const user = await swr(apiGetUser)({id});
    return <>{user.name}</>;
}

Additional context
Implemented as experimental_swr first.

Possible Implement

const swr = (asyncFunction) => {
    const previousPromiseMap = new Map();
    return (params) => {
        if (sameParamsHasCalledInShortTimeBefore) {
            return previousPromise;
        }
        return asyncFunction(params);
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions