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: pass arg on options.onSuccess of useSWRMutation and useSWR #2591

Closed

Conversation

benevbright
Copy link

@benevbright benevbright commented Apr 29, 2023

It's useful when you build your custom hook with useSWRMutation.

const useUpdatePost = () => {
  const { trigger, isMutating } = useSWRMutation(
    key,
    (url, { arg }) => fetch(url, { method: "POST", body: arg }),
    {
      onSuccess: (data, key, config) => {
        // I want to do something with response data and arg here
        // but I can't access fetcher's arg
      },
    }
  );
  return { trigger, isMutating };
};

Currently, there is no way to access arg in onSuccess. Thus let's pass arg on onSuccess.

@codesandbox-ci
Copy link

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.

Latest deployment of this branch, based on commit 08dbebd:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
SWR-SSR Configuration

@@ -38,7 +38,8 @@ export type SWRMutationConfiguration<
key: string,
config: Readonly<
SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey, SWRData>
>
>,
arg: any

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benevbright how about using the ExtraArg type instead of the any type?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for late response. I found that my PR is duplicated: #2286
will close this one.

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.

2 participants