R
Refine

ask-any-question

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

Iinland-turquoise8/19/2023
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?
Kkapa.ai8/19/2023
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: ----------------------
Kkapa.ai8/19/2023
<@711407420718579741> kapa.ai is still learning and improving, please let us know how it did by reacting below
Rrare-sapphire8/19/2023
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.
Iinland-turquoise8/19/2023
Thanks @aliemirs for your thoughtful answer. I know exactly what to do now.

Looking for more? Join the community!

R
Refine

ask-any-question

Join Server
Recommended Posts
refreshing useSelecthow to make useSelect refresh when a state variable is setHow to Handle Routes for Resources that are RelatedI have organizations that have many deployments and users, and deployments have many devices How soSetup Relationships Between Resources for InferencerSo my app has an organization resource and a deployment resource; deployments belong to an organizatErrors When Trying to Delete a ResourceOk, so in my List view, I added column actions for each row, where one of the actions is to delete tStrapi v4 Data Provider ExampleHello, I got problem with file uploading. I downloaded your example and made collection in strapi ad404 Error Create PageSo I created a new route for my organizations for the create resource: ``` { name: "organizations"How do I use `useEditableTable` with my own custom data fetcher/update logic?Essentially, I want to use the useEditableTable hook without a resource. I have backend call that caIs there any NX plugin to generate Refine templates in existing NX projects?Is there any NX plugin to generate Refine templates in existing NX projects?What type is error for onError within AuthProvider?I have an onError function in my custom authProvider.ts: ``` onError: async (error) => { consoleResource for "/"Hiya! I want to have a "home" page at the "/" route. For the life of me I cannot figure out how Redirect to resource/show/:id After Creating Said ResourceHello! I'd like to redirect the user to the newly-created resource after they've created it. How cWhy does my List page generate so many queries?This is my code for my list page (listing organizations): ``` import React from "react"; import { usProblems Using Axios to Query from AWS API GatewayHi All, I am going through the docs and I setup a manual data provider so i can provide my AWS CognDynamic form fieldCan u show an example where i can add multiple fields dynamically??