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

feat: add extra arg in onSuccess, onError #2286

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

Lee-sungheon
Copy link

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

async onSuccess(data, key, config, extraArg) {
  ...

  await mutate('otherKey', extraArg.pk);
},
async onError(error, key, config, extraArg) {
  ...

  await mutate('otherKey', extraArg.pk);
},

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 13, 2023

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.

mutation/types.ts Outdated Show resolved Hide resolved
mutation/types.ts Outdated Show resolved Hide resolved
@Lee-sungheon Lee-sungheon requested review from promer94 and removed request for shuding and huozhi March 14, 2023 15:25
@promer94
Copy link
Collaborator

promer94 commented Mar 14, 2023

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 onSuccess by trigger. It's more flexible.

Would this solve your problem ?

@promer94 promer94 requested review from huozhi and shuding March 14, 2023 15:57
@Lee-sungheon
Copy link
Author

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)
    }
  })
}

@Lee-sungheon Lee-sungheon marked this pull request as draft February 24, 2024 10:23
@Lee-sungheon Lee-sungheon marked this pull request as ready for review February 24, 2024 10:24
@Lee-sungheon
Copy link
Author

@huozhi @shuding @promer94
Can you check the PR?
This is a must-have feature for me.

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.

None yet

2 participants