-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
how to use post method and pass params in swr? #93
Comments
const fetcher = params => url => post(url, params) This way? |
SWR is more intended to get data, not update it, usually you have another function to update and after it you will run mutate to update the cache and trigger a revalidation (SWR will fetch it again) |
This example https://github.com/zeit/swr/tree/master/examples/optimistic-ui does something similar, update and then revalidate |
UPDATE 2022.6: SWR 2.0 beta now has a dedicated API for one-off requests such as POST. Docs and examples can be found here: https://github.com/vercel/swr/releases/tag/2.0.0-beta.0 It also supports many good things such as cache population, error rollback and race condition prevention. Original answer: function App() {
const { data } = useSWR('/api/data', fetch) // <--------------GET
return <>
{data}
<button onClick={() => {
fetch('/api/data', { method: 'POST', body: ... }) // <---POST
}}>update data</button>
</>
} You can just use
But speaking of revalidation/mutation, it's still a thing for allowing POST together with SWR. Currently we need to do this: mutate('/api/data', newData, false) // local mutate without revalidation
await sendPost('/api/data', newData) // POST request
trigger('/api/data') // revalidate Maybe we can simplify it to something like: mutate('/api/data', newData, sendPost) which will do the exact same thing as above. |
Maybe it'd be worth checking out |
I have a use case with firebase onCall function(which it's method is POST). Because it supports authentication by default, I have to use this workaround.
Then I can use
My function is onCall because it needs to verify user before sending back data. Any idea on improving this? @quietshu |
@iammarkps can you show me a POST example you have without using SWR? |
I always use it that way. But I'm just curious that, can I have only 1 global function to use firebase callable function? Now I have to implement 3 different functions with same logic. https://github.com/programming-in-th/programming.in.th/blob/master/src/utils/fetcher.ts Because firebase callable function receives 2 args, not 1 like normal GET request(Just url of API). PS.Pardon my bad english |
I've used to do this when I was using redux (It's POST method). and this for normal data fetching (Still POST method) But now I've converted it to normal http GET not firebase callable anymore. https://github.com/programming-in-th/cloud_functions/blob/master/functions/src/tasks.ts#L4 So it works perfectly with SWR. But I can't convert this to http GET, because I have to check user right to access the code in line 187. So I need to depend on firebase request context. @quietshu |
I want to do this
|
@iammarkps thanks for elaborating! 👍 Now I can understand your use case, it makes perfect sense.
And yes! This feature will solve the problem: #98 We just shipped it to useSWR([type, param], fetchFromFirebase) But keep in mind it compares arguments shallowly, so keep in mind to memorize the object: const param = useMemo(() => ({ submission_id: id }), [id])
useSWR(['getDetailedSubmissionData', param], fetchFromFirebase) |
@quietshu Thank you! I've just improved it with your suggestion in this commit |
@iammarkps looks great! |
So, this should be work in the newest version (dependent data and multiple params): const { data: user } = useSWR('/api/user')
const params = useMemo(() => {
return { username: user && user.username }
}, [user])
const profile = useSWR(() => ['/api/profile', params]) Because I'm trying this and I'm getting an infinite loop always 😕 |
@pedronauck oh that's because you're using a function which returns an array here: const profile = useSWR(() => ['/api/profile', params]) This case is not yet covered (thanks for reporting!), for now you can just use an array: const profile = useSWR(['/api/profile', params]) |
I Used like this and it worked for me, please let me know If I’m missing something
|
Hi! Great library, I had a quick question:
Right now, it's fetching whenever any attribute of Thanks |
You need to pass every attribute inside |
mark |
Answers here are assuming that POST = data mutation Multiple arguments help a lot here, the second argument can be "fetch" call options: https://swr.vercel.app/docs/arguments#multiple-arguments |
well, this works for me:
|
No description provided.
The text was updated successfully, but these errors were encountered: