T
TanStack4y ago
ambitious-aqua

Is onSuccess recreated on each render?

I have a custom mutation hook like this and I was wondering if there could be any cases where prevHemoglobinValue would be outdated in the onSuccess callback?
export const useEditHemoglobinEntryMutation = () => {
const dispatch = useDispatch();
const queryClient = useQueryClient();

const prevHemoglobinValue = useSelector(selectors.hemoglobinData.getLastHemoglobinValue);

return useMutation(
async (payload: HemoglobinLevel) => {
await Api.editHemoglobinValue(payload.id, payload);
},
{
onSuccess: async (_, entry) => {
maybeShowModal(prevHemoglobinValue, entry.value, dispatch);
},
},
);
};
export const useEditHemoglobinEntryMutation = () => {
const dispatch = useDispatch();
const queryClient = useQueryClient();

const prevHemoglobinValue = useSelector(selectors.hemoglobinData.getLastHemoglobinValue);

return useMutation(
async (payload: HemoglobinLevel) => {
await Api.editHemoglobinValue(payload.id, payload);
},
{
onSuccess: async (_, entry) => {
maybeShowModal(prevHemoglobinValue, entry.value, dispatch);
},
},
);
};
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?