RTK Mutation Query causing an infinite loop

Viewed 33
const [createLinkTokenTrigger, createLinkTokenResult] = useCreateLinkTokenMutation();

createLinkTokenTrigger({ productType }, { skip: !isSubscriber });

Does using RTK mutation trigger need to be put in a UseEffect()? It solves my problem of infinite render loop but I didn't see useEffect being used in examples and wondering if something else might be wrong with my code.

When I had same mutation query as regular RTK get query this didn't happen. But I recently switched it to a mutation as it's a POST request.

1 Answers

A mutation will execute the moment you execute the trigger function. You definitely cannot call that in a render function, just like any other side effect. You probably also should only put it into a useEffect only in edge cases and usually call it in event handlers like onClick - after all, it will trigger a change on the server and that should not only happen because the user has a webpage open, but because the user did some kind of interaction.

Related