useMutation()
The Hook enables you to perform asynchronous data mutation operations, such as POST, PUT, PATCH, or DELETE requests. It not only handles the loading state, but also provides an APIs for optimistic updates and error handling. See the TanStack useMutation(...) ๐ด documentation for more details.
const query = api.<service>.<operation>.useMutation(
parameters,
mutationOptions
)
Argumentsโ
-
parameters?: { path, query, header }- Optional, OpenAPI request parameters for the query, strictly-typed โจ
- When
parametersprovided, it will be used to generate themutationKey
-
mutationOptions?: UseMutationOptions- Optional, represents the options of the useMutation(...) ๐ด Hook
- When
api.<srv>.<op>.useMutation(parameters)is used (with the predefined parameters),mutationKeywill contain theparametersand operation schema - When
api.<srv>.<op>.useMutation(undefined)is used (without predefined parameters),mutationKeywill contain just operation schema mutationOptions.mutationFncould be provided to override the defaultmutationFnused by Qraft, strictly-typed โจmutationOptions.mutationKeycould be provided to override the generated key fromparameters, strictly-typed โจ
Returnsโ
mutation: MutationResult- Object from the useMutation(...) ๐ด Hook
mutation.mutate(parameters | {parameters, body})initiates the requestmutation.variableswill contain theparametersandbodyof the request, after callingmutation.mutate(...).
Examplesโ
- With parameters ๐
- No parameters ๐
When the parameters are known, the useMutation hook can be used with parameters
to generate the mutationKey.
mutation.mutate(...)will accept thebodyof the request as the single argument.
src/EntityEditForm.tsx
import { createAPIClient } from './api'; // generated by OpenAPI Qraft
import { requestFn } from '@openapi-qraft/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
const queryClient = new QueryClient();
const api = createAPIClient({
requestFn,
queryClient,
baseUrl: 'https://api.sandbox.monite.com/v1',
});
function EntityEditForm({ entityId }: { entityId: string }) {
const mutation = api.entities.entitiesIdDocuments.useMutation({
path: {
entity_id: entityId
},
header: {
'x-monite-version': '2023-09-01',
},
});
return (
<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
/**
* `mutation.mutate(...)` triggers the submission of a request:
* ###
* POST /entities/3e3e-3e3e-3e3e/documents
* x-monite-version: 2023-09-01
*
* {"company_tax_id_verification": ["verification-id"]}
**/
mutation.mutate({
company_tax_id_verification: [
`${formData.get('company_tax_id_verification')}`,
],
});
}}
>
<input name="company_tax_id_verification" />
<button disabled={mutation.isPending}>Submit</button>
</form>
);
}
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<EntityEditForm entityId="123" />
</QueryClientProvider>
);
}
When the parameters are not known, the useMutation hook can be used without parameters.
mutation.mutate(...)will accept the object withparametersandbodyof the request as the single argument.
src/EntityForm.tsx
import { createAPIClient } from './api'; // generated by OpenAPI Qraft
import { requestFn } from '@openapi-qraft/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
const queryClient = new QueryClient();
const api = createAPIClient({
requestFn,
queryClient,
baseUrl: 'https://api.sandbox.monite.com/v1',
});
function EntityForm() {
const mutation = api.entities.entitiesIdDocuments.useMutation();
return (
<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
/**
* `mutation.mutate(...)` triggers the submission of a request:
* ###
* POST /entities/3e3e-3e3e-3e3e/documents
* x-monite-version: 2023-09-01
*
* {"company_tax_id_verification": ["verification-id"]}
**/
mutation.mutate({
parameters: {
path: {
entity_id: `${formData.get('entity_id')}`
},
header: {
'x-monite-version': '2023-09-01',
},
},
body: {
company_tax_id_verification: [
`${formData.get('company_tax_id_verification')}`,
],
}
});
}}
>
<input name="entity_id" readOnly={mutation.isPending} />
<input name="company_tax_id_verification" readOnly={mutation.isPending} />
<button disabled={mutation.isPending}>Submit</button>
</form>
);
}
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<EntityForm />
</QueryClientProvider>
);
}