stormy-gold
stormy-gold12mo ago

What parameter should I set to make mutate() optionally return the created data?

I would like to create a new resource and navigate to the edit view for that resource.
mutate(
{
resource: "deal",
values: {
...
},
// Any parameter here to signal to the data provider that I need the newly created resource?
},
},
{
onError: (error) => {
...
},
onSuccess: (data) => {
// My data provider may return undefined data.data.
go({
to: `/resource/${data.data.id}`,
type: "push",
});
},
}
mutate(
{
resource: "deal",
values: {
...
},
// Any parameter here to signal to the data provider that I need the newly created resource?
},
},
{
onError: (error) => {
...
},
onSuccess: (data) => {
// My data provider may return undefined data.data.
go({
to: `/resource/${data.data.id}`,
type: "push",
});
},
}
When I call useCreate's mutate() function, onSuccess could optionally contain the data I need to go() to the edit view. However, by default my data provider performs the create without a following select. I would like to signal to my data provider to optionally select the created resource. What is the canonical way to pass this option in Refine?
4 Replies
kapa.ai
kapa.ai12mo ago
Hi @imedemi. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the provided extracts, there isn't a direct way to signal the data provider to select the created resource after the create operation. The useCreate hook's mutate function doesn't seem to support such a parameter. However, you can handle this in the onSuccess callback of the mutate function. Here, you can navigate to the edit view of the newly created resource. The data parameter of the onSuccess callback should contain the data of the newly created resource. Here's an example:
const { mutate } = useCreate();

mutate(
{
resource: "deal",
values: {
// values for the new resource
},
},
{
onSuccess: (data) => {
// navigate to the edit view of the new resource
go({
to: `/resource/${data.data.id}`,
type: "push",
});
},
}
);
const { mutate } = useCreate();

mutate(
{
resource: "deal",
values: {
// values for the new resource
},
},
{
onSuccess: (data) => {
// navigate to the edit view of the new resource
go({
to: `/resource/${data.data.id}`,
type: "push",
});
},
}
);
In this example, go is a function that navigates to the given path. You need to implement this function according to your routing library. Please note that the actual structure of the data object depends on your data provider. In this example, it's assumed that the data object has a data property which is an object representing the newly created resource, and this object has an id property. For more information, refer to the useCreate documentation (https://refine.dev/docs/api-reference/core/hooks/data/useCreate). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai12mo ago
<@711407420718579741> kapa.ai is still learning and improving, please let us know how it did by reacting below
genetic-orange
genetic-orange12mo ago
Hey @imedemi, this capability depends on your data provider. In custom ones you can use the meta property to pass additional data to the data provider and inside the data provider, you can use that value to select or not after create. BTW you need to use the pessimistic mutation mode (default one) to have the response in the onSuccess method, otherwise it will be undefined. For example we have meta.select in @refinedev/supabase that you can use in useCreate and other similar hooks to do a select query after the mutation.
stormy-gold
stormy-gold12mo ago
Thanks @aliemirs for your thoughtful answer. I know exactly what to do now.