fair-rose
fair-rose4w ago

redirect in onMutationSuccess

Hi, How can I redirect to one of the routes from history onMutationSuccess callback?
2 Replies
kapa.ai
kapa.ai4w ago
Hi @Pankaj. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To redirect to one of the routes from history in the onMutationSuccess callback, you can use the useGo hook or the useNavigation hook provided by refine. Below are examples of how to achieve this. Using useGo Hook You can use the useGo hook to programmatically navigate to a different route. Here is an example:
import { useGo } from "@refinedev/core";

const go = useGo();
const result = useModalForm({
refineCoreProps: {
redirect: false,
onMutationSuccess: () => {
go({ to: '/my-path' });
}
}
});
import { useGo } from "@refinedev/core";

const go = useGo();
const result = useModalForm({
refineCoreProps: {
redirect: false,
onMutationSuccess: () => {
go({ to: '/my-path' });
}
}
});
Source (https://discord.com/channels/837692625737613362/1225217258863722616) Using useNavigation Hook Alternatively, you can use the useNavigation hook to navigate back in history or to a specific route. Here is an example:
import { useNavigation } from "@refinedev/core";

const { goBack } = useNavigation();
const { redirect } = useForm({
redirect: false,
onMutationSuccess: () => {
if (IS_HISTORY_EMPTY) {
redirect("list");
} else {
goBack();
}
}
});
import { useNavigation } from "@refinedev/core";

const { goBack } = useNavigation();
const { redirect } = useForm({
redirect: false,
onMutationSuccess: () => {
if (IS_HISTORY_EMPTY) {
redirect("list");
} else {
goBack();
}
}
});
Source (https://github.com/refinedev/refine/issues/1806) Additional Resources - useGo Hook Documentation (https://refine.dev/docs/routing/hooks/use-go/#usage) - useForm Hook Documentation (https://refine.dev/docs/ui-integrations/ant-design/hooks/use-form/#onmutationsuccess) These examples should help you set up redirection in the onMutationSuccess callback based on your requirements. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@1148728588506501242> kapa.ai is still learning and improving, please let us know how it did by reacting below