-
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
feat: add extra arg in onSuccess, onError #2286
base: main
Are you sure you want to change the base?
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
I realize there is a alternative way to achieve this. const { trigger, isMutating } = useSWRMutation('/api/user', sendRequest)
trigger(arg, { onSuccess: () => {
mutate('other key', arg)
}}) You could config Would this solve your problem ? |
Good alternative way but I want to create a custom hook using useSWRMutation. When a custom hook is created in a way that uses a trigger, not only does the code become complicated, but it also becomes difficult to further customize the trigger. export const useSendRequest = () => {
const { trigger, ...others } = useSWRMutation('/api/user', sendRequest)
const myTrigger = (arg) => trigger(arg, { onSuccess: () => {
mutate('other key', arg)
}})
return {
trigger: myTrigger,
...others
}
} However, when a custom hook is created as follows, not only the readability of the code is improved, but also the trigger is additionally customizable. export const useSendRequest = () => {
return useSWRMutation('/api/user', sendRequest, {
onSuccess: (data, key, config, extraArg) => {
mutate('other key', extraArg)
}
})
} |
Hi!
While using swr 2.0, I felt uncomfortable that extgraArg could not be used in onSuccess and onError.
So I suggest you can use extraArg in onSuccess and onError like this