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

use swr for fetching and mutations #24

Merged
merged 10 commits into from
Feb 20, 2024
Merged

use swr for fetching and mutations #24

merged 10 commits into from
Feb 20, 2024

Conversation

technophile-04
Copy link
Member

@technophile-04 technophile-04 commented Feb 20, 2024

Description

We could completely close this if it seems overpowered for the use case 🙌

Demo video of admin action :

Screen.Recording.2024-02-20.at.12.23.21.PM.mov

Notes :

Notes

For fetching data GET request :

  1. We just need to pass the API endpoint to useSWR hook and this also acts as a key
  2. The useSWR will make the GET request with fetch and returns response data along with isLoading, isError etc
  3. Whever an component mounts it will make the request (we can disbale it by revalidateOnMount: false )

eg :

const { data, isLoading } = useSWR<{ data: GrantData[] }>("/api/grants/review");

For POST :

  1. 1st arg Pass the api endpoint
  2. 2nd arg postMutationFetcher utility, (we could also pass body type to as generic)
  3. call the trigger function by passing it the body
const { trigger: postReviewGrant, isMutating: isPostingNewGrant } = useSWRMutation(
  `/api/grants/${grant.id}/review`,
  postMutationFetcher<ReqBody>,
);

// In some async function we can call trigger to make `POST` request : 
await postReviewGrant({ signer: address, signature, action });
await mutate("/api/grants/review");
//  mutate comes from : const { mutate } = useSWRConfig();

What this mutate function from useSWRConfig does is it internally makes the GET request for url/ key and updates its store maintained for that key / url

Also notice in demo video although we made GET request to /api/grants/review api endpoint after posting review, it didn't show loading spinner and automatically removed the item. I think they achieved this by "mutating" the store for that key / url properly. Lol I really this experience


Overall I love the experience and makes life a bit simple(like not creating useEffects, isLoading etc), but 100% agree it might be a bit overpowered

closes #22

Copy link

vercel bot commented Feb 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
grants-bg ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 20, 2024 11:21am

@carletex
Copy link
Contributor

Thank you very much for this <3 Top level stuff ;)

We could completely close this if it seems overpowered for the use case 🙌

Maybe.... but It's a great excuse to use swr (never used that lib before).... and we can learn it for future builds.

@carletex carletex merged commit d8e4478 into main Feb 20, 2024
3 checks passed
@carletex carletex deleted the useSwr branch March 6, 2024 10:56
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

Successfully merging this pull request may close these issues.

Explore using SWR
2 participants