Replies: 4 comments 2 replies
-
I believe SWR (stands for stale-while-revalidate) is purposed to fetch data only. Personally, I don't see a clean way to match your use case with You'd have to bypass many features to repurpose it for mutating
You could fetch the votes using
|
Beta Was this translation helpful? Give feedback.
-
Hi @scucchiero Thx for your answer.
And in my onClick function I call |
Beta Was this translation helpful? Give feedback.
-
Here's another way to do it. Use SWR for GET requests and react-use's function useCreate() {
const { mutate } = useSWRConfig();
// Use react-use's useAsyncFn for post requests
return useAsyncFn(async () => {
// Call API
// ...
// Trigger a refresh
mutate("/api/list")
})
}
function useList() {
// Use SWR to fetch data
return useSWR("/api/list", fetcher);
} |
Beta Was this translation helpful? Give feedback.
-
Hi all! There is a new hook A quick look: import useSWRMutation from 'swr/mutation'
async function sendRequest(url, { arg }) {
return fetch(url, {
method: 'POST',
body: JSON.stringify(arg)
})
}
function App() {
const { trigger } = useSWRMutation('/api/user', sendRequest)
return <button onClick={() => {
trigger({ username: 'johndoe' })
}}>Create User</button>
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I have an upvote button that calls my upvote function with argument of type
UPVOTE
orDOWNVOTE
. Then I make a post request to my api.I can't use
useSWR
in this function since hooks cant be used inside functions.How can I :
useSWR
I want to use swr for deduping. So when the user clicks 10 times very fast on the vote button it just refetches it after few seconds and just mutating the local cache.
Thank you guys!
Beta Was this translation helpful? Give feedback.
All reactions