Skip to content
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

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

Open
dancerphil opened this issue Feb 24, 2023 · 0 comments
Open

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

dancerphil opened this issue Feb 24, 2023 · 0 comments
Assignees

Comments

@dancerphil
Copy link
Owner

dancerphil commented Feb 24, 2023

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);
    }
}
@dancerphil dancerphil self-assigned this Feb 24, 2023
@dancerphil dancerphil changed the title options: {swr: true} Support React 19 with options: {swr: true} Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant