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

Mutation option callbacks are not firing if mutation is not mounted #95

Open
jljorgenson18 opened this issue Oct 11, 2024 · 0 comments
Open

Comments

@jljorgenson18
Copy link

jljorgenson18 commented Oct 11, 2024

We are currently migrating our business logic to Jotai and we are seeing an issue where creating a mutation atom and only calling it from inside of a write atom is causing the mutation callbacks to not fire.

If you have atoms like this

const someMutationAtom = atomWithMutation((get) => {
  return {
    mutationFn: fakeMutationCall,
  };
});

const fakePublishAtom = atom(null, async (get, set) => {
  await get(someMutationAtom).mutateAsync(undefined, {
    onSuccess: () => {
      set(publishCountAtom, (prev) => prev + 1);
    },
  });
});

Then onSuccess will not fire unless someMutationAtom is mounted somewhere. I believe internally there is a listener attached to the mutation observer while mounted and I would expect that listener to be active while a query is occurring.

I should add that passing options into the atomWithMutation callback are working just fine. However, there are many cases where we want to custom options on a mutation by mutation basis.

See https://codesandbox.io/p/sandbox/jotai-react-query-repro-forked-99trmz for an example. If you comment in the mutation hook, it starts working.

@jljorgenson18 jljorgenson18 changed the title Mutation option callback are not firing if mutation is not mounted Mutation option callbacks are not firing if mutation is not mounted Oct 11, 2024
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

No branches or pull requests

1 participant