Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useMutation.js

158 lines3.2 KB

1import React from 'react'
2​
3//
4​
5import { useConfigContext } from './config'
6import {
7 statusIdle,
8 statusLoading,
9 statusSuccess,
10 statusError,
11 useGetLatest,
12 Console,
13 uid,
14 useMountedCallback,
15 noop,
16} from './utils'
17​
18const getDefaultState = () => ({
19 status: statusIdle,
20 data: undefined,
21 error: null,
22})
23​
24const actionReset = {}
25const actionLoading = {}
26const actionResolve = {}
27const actionReject = {}
28​
29function mutationReducer(state, action) {
30 if (action.type === actionReset) {
31 return getDefaultState()
32 }
33 if (action.type === actionLoading) {
34 return {
35 status: statusLoading,
36 }
37 }
38 if (action.type === actionResolve) {
39 return {
40 status: statusSuccess,
41 data: action.data,
42 }
43 }
44 if (action.type === actionReject) {
45 return {
46 status: statusError,
47 error: action.error,
48 }
49 }
50 throw new Error()
52​
53export function useMutation(mutationFn, config = {}) {
54 const [state, unsafeDispatch] = React.useReducer(
55 mutationReducer,
56 null,
57 getDefaultState
58 )
59​
60 const dispatch = useMountedCallback(unsafeDispatch)
61​
62 const getMutationFn = useGetLatest(mutationFn)
63​
64 const getConfig = useGetLatest({
65 ...useConfigContext(),
66 ...config,
67 })
68​
69 const latestMutationRef = React.useRef()
70​
71 const mutate = React.useCallback(
72 async (
73 variables,
74 { onSuccess = noop, onError = noop, onSettled = noop, throwOnError } = {}
75 ) => {
76 const config = getConfig()
77​
78 const mutationId = uid()
79 latestMutationRef.current = mutationId
80​
81 const isLatest = () => latestMutationRef.current === mutationId
82​
83 dispatch({ type: actionLoading })
84​
85 let snapshotValue
86​
87 try {
88 snapshotValue = await config.onMutate(variables)
89​
90 let data
91​
92 if (isLatest()) {
93 data = await getMutationFn()(variables)
94 }
95​
96 if (isLatest()) {
97 await onSuccess(data, variables)
98 }
99​
100 if (isLatest()) {
101 await config.onSuccess(data, variables)
102 }
103​
104 if (isLatest()) {
105 await onSettled(data, null, variables)
106 }
107​
108 if (isLatest()) {
109 await config.onSettled(data, null, variables)
110 }
111​
112 if (isLatest()) {
113 dispatch({ type: actionResolve, data })
114 }
115​
116 return data
117 } catch (error) {
118 if (isLatest()) {
119 Console.error(error)
120 await onError(error, variables, snapshotValue)
121 }
122​
123 if (isLatest()) {
124 await config.onError(error, variables, snapshotValue)
125 }
126​
127 if (isLatest()) {
128 await onSettled(undefined, error, variables, snapshotValue)
129 }
130​
131 if (isLatest()) {
132 await config.onSettled(undefined, error, variables, snapshotValue)
133 }
134​
135 if (isLatest()) {
136 dispatch({ type: actionReject, error })
137​
138 if (throwOnError ?? config.throwOnError) {
139 throw error
140 }
141 }
142 }
143 },
144 [dispatch, getConfig, getMutationFn]
145 )
146​
147 const reset = React.useCallback(() => dispatch({ type: actionReset }), [
148 dispatch,
149 ])
150​
151 React.useEffect(() => {
152 if (getConfig().useErrorBoundary && state.error) {
153 throw state.error
154 }
155 }, [getConfig, state.error])
156​
157 return [mutate, { ...state, reset }]