I am using react-query hooks, my goal is to send the updated state to the database and receive the updated state as well.
My object is a workout diary that displays exercies on each given day:
{
name: "my workout diary",
currentDay: 0,
days: [
{dayName: "monday", exercises: {...}},
{dayName: "day 2", exercises: {...}},
{dayName: "rest day", exercises: {...}}
]
}
The number in currentDay
field indicates what day currently is being rendered on the screen from days
array.
I have 2 buttons that move day back or forward using useMutation
hook:
const query = useFirestoreDocument([...],ref);
const mutation = useFirestoreDocumentMutation(
ref,
{ merge: true },
{
onSuccess() {
queryClient.invalidateQueries([...]);
},
}
);
const workoutplan = query.data?.data();
const moveDayBack = () => {
if (workoutplan && !mutation.isLoading) {
workoutplan.currentDay > 0 &&
mutation.mutate({
currentDay: increment(-1),
});
}
};
Everything works just the way I want to if I do it slowly, but if I spam movedayback function fails the conditional check and sends the -1
to the server then goes back to 0
, but that causes fatal error, because negative array index will return undefined
.
Why are my conditional checks failing and how do I prevent that?
Aucun commentaire:
Enregistrer un commentaire