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

Feature Request: Callback to Reset Mutation Status #1455

Closed
bradharms opened this issue Aug 30, 2021 · 4 comments · Fixed by #1476
Closed

Feature Request: Callback to Reset Mutation Status #1455

bradharms opened this issue Aug 30, 2021 · 4 comments · Fixed by #1476
Labels
enhancement New feature or request rtk-query
Milestone

Comments

@bradharms
Copy link

I would like for mutation hooks to return a callback to reset mutation status back to their initial state so that the isSuccess, isError, etc. flags are all false.

The rational is for cases like modals whose contents change in response to mutation status change where the modal may need to be re-opened after closing. It's possible to get this behavior by restructuring your component hierarchy so that the hook's component gets unmounted, or by abstracting access to state, but the first isn't always possible and the second is messier than simply resetting the mutation status as a counter to firing the mutation's trigger function.

Example:

import React from 'react';
import {useAddToCartMutation} from './api';
import Modal from './Modal';

interface Props {
  isOpen?: boolean;
  onClose?: () => void; // Consumer will use this to set isOpen to false
}

const AddToCartModal = (props: Props) => {
  const [addToCart, addToCartMutation] = useAddToCartMutation();

  const handleAddToCart = () => { addToCart(); }
  
  const handleClose = () => { setIsOpen(false); props.onClose(); }

  useEffect(() => {
   if (props.isOpen) {
     addToCartMutation.reset(); // <--- HERE
   }
  }, [props.isOpen]);

  return <Modal open={props.isOpen}>
    {addToCartMutation.isFetching ? <div>Adding to cart...</div> :
     addToCartMutation.isError ? <><div>An error occurred</div><div><button onClick={handleClose}>Close</div> :
     addToCartMutation.isSuccess ? <><div>Thank you!</div><div><button onClick={handleClose}>Close</div> :
     <button onClick={handleAddToCart}>Add to Cart</button>
  </Modal>
};
@phryneas
Copy link
Member

Yeah we've had that request a few times, so I'll put it on the list.

@phryneas phryneas added the enhancement New feature or request label Aug 30, 2021
@sebastienlabine
Copy link

is there a workaround right now to reset the mutation state?

@phryneas
Copy link
Member

phryneas commented Sep 2, 2021

Triggering a new mutation. Otherwise you'll just have to track that as a local state variable yourself in some way.

@markerikson
Copy link
Collaborator

Available in the 1.7 betas.

@markerikson markerikson added this to the 1.7 milestone Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rtk-query
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants