Skip to content

Commit b12bbd7

Browse files
authored
docs(dev): Update frontend dev doc examples to use fetchMutation (#14402)
1 parent 20edde3 commit b12bbd7

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

develop-docs/frontend/network-requests.mdx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,23 @@ Mutations, unlike queries, do not fire automatically. Instead of `data`, it retu
167167
Let’s say you have a button that creates a project with `POST /organizations/<org>/projects/`. You will need to define the response type (`CreateProjectResponse` in this example) as well as the shape of the object you will pass to the mutation function (`CreateProjectVariables` in this example). You can then use the `mutate` function to trigger the mutation:
168168

169169
```tsx
170-
import {useMutation} from 'sentry/utils/queryClient';
170+
import {fetchMutation, useMutation} from 'sentry/utils/queryClient';
171171

172172
type CreateProjectResponse = {id: string; name: string};
173173
type CreateProjectVariables = {name: string; orgSlug: string};
174174

175175
function Component() {
176-
const api = useApi();
177-
178176
const {mutate} = useMutation<
179177
CreateProjectResponse,
180178
RequestError,
181179
CreateProjectVariables
182180
>({
183181
...options,
184182
mutationFn: ({name, orgSlug}: CreateProjectVariables) =>
185-
api.requestPromise(`/organizations/${orgSlug}/projects/`, {
183+
fetchMutation({
184+
url: `/organizations/${orgSlug}/projects/`,
186185
method: 'POST',
187-
data: {name},
186+
data: {name}
188187
}),
189188
onSuccess: response => {
190189
addSuccessMessage(`Successfully created project ${response.name}`);
@@ -215,13 +214,13 @@ export function makeFetchProjectQueryKey({id}): ApiQueryKey {
215214

216215
// useUpdateProjectNameOptimistic.tsx
217216
function useUpdateProjectNameOptimistic(incomingOptions: UpdateProjectOptions) {
218-
const api = useApi();
219217
const queryClient = useQueryClient();
220218

221219
const options: Options = {
222220
...incomingOptions,
223221
mutationFn: ({id, name}) => {
224-
return api.requestPromise(`/projects/${id}/`, {
222+
return fetchMutations({
223+
url: `/projects/${id}/`,
225224
method: 'PUT',
226225
data: {name},
227226
});

0 commit comments

Comments
 (0)