Skip to content

Commit d7b132e

Browse files
authored
refactor(react,vue): rename mutate functions to mutate/mutateAsync (#4878)
* chore: tweaks * chore: up * chore: changeset * chore: react * chore: vue * chore: tweaks * chore: audit * chore: up * docs: up
1 parent 7e58fe1 commit d7b132e

File tree

117 files changed

+1286
-1059
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+1286
-1059
lines changed

.changeset/eager-jobs-rhyme.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
"wagmi": minor
3+
"@wagmi/vue": patch
4+
---
5+
6+
Deprecated custom mutate function names and renamed to `mutate`/`mutateAsync` to reduce destructure key renaming fatigue and align with TanStack Query terminology.
7+
8+
**Before**
9+
10+
Had to destructure hook result and often rename keys when using multiple of the same hook. Could decide not to destructure, but syntax becomes awkward for mutate functions (e.g. `connect.connect` or `connect.connectAsync`).
11+
12+
```ts
13+
const { connect, isPending: connectIsPending } = useConnect()
14+
const { writeContract: transfer, error: transferError, isPending: transferIsPending } = useWriteContract()
15+
const { writeContract: approve, error: approveError } = useWriteContract()
16+
```
17+
18+
**After**
19+
20+
Allows you to name the hook result whatever you want and not worry about also renaming properties.
21+
22+
```ts
23+
const connect = useConnect() // connect.isPending
24+
const transfer = useWriteContract() // transfer.mutate, transfer.error, transfer.isPending
25+
const approve = useWriteContract() // approve.mutate, approve.error
26+
```

packages/react/src/hooks/codegen/createUseWriteContract.ts

Lines changed: 132 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,81 @@ export type CreateUseWriteContractReturnType<
6363
wagmi_UseWriteContractReturnType<config, context>,
6464
'writeContract' | 'writeContractAsync'
6565
> & {
66+
mutate: <
67+
const abi2 extends abi,
68+
name extends functionName extends ContractFunctionName<
69+
abi,
70+
stateMutability
71+
>
72+
? functionName
73+
: ContractFunctionName<abi, stateMutability>,
74+
args extends ContractFunctionArgs<abi2, stateMutability, name>,
75+
chainId extends config['chains'][number]['id'],
76+
>(
77+
variables: Variables<
78+
abi2,
79+
functionName,
80+
name,
81+
args,
82+
config,
83+
chainId,
84+
address
85+
>,
86+
options?:
87+
| MutateOptions<
88+
WriteContractData,
89+
WriteContractErrorType,
90+
WriteContractVariables<
91+
abi2,
92+
name,
93+
args,
94+
config,
95+
chainId,
96+
// use `functionName` to make sure it's not union of all possible function names
97+
name
98+
>,
99+
context
100+
>
101+
| undefined,
102+
) => void
103+
mutateAsync: <
104+
const abi2 extends abi,
105+
name extends functionName extends ContractFunctionName<
106+
abi,
107+
stateMutability
108+
>
109+
? functionName
110+
: ContractFunctionName<abi, stateMutability>,
111+
args extends ContractFunctionArgs<abi2, stateMutability, name>,
112+
chainId extends config['chains'][number]['id'],
113+
>(
114+
variables: Variables<
115+
abi2,
116+
functionName,
117+
name,
118+
args,
119+
config,
120+
chainId,
121+
address
122+
>,
123+
options?:
124+
| MutateOptions<
125+
WriteContractData,
126+
WriteContractErrorType,
127+
WriteContractVariables<
128+
abi2,
129+
name,
130+
args,
131+
config,
132+
chainId,
133+
// use `functionName` to make sure it's not union of all possible function names
134+
name
135+
>,
136+
context
137+
>
138+
| undefined,
139+
) => Promise<WriteContractData>
140+
/** @deprecated use `mutate` instead */
66141
writeContract: <
67142
const abi2 extends abi,
68143
name extends functionName extends ContractFunctionName<
@@ -100,6 +175,7 @@ export type CreateUseWriteContractReturnType<
100175
>
101176
| undefined,
102177
) => void
178+
/** @deprecated use `mutateAsync` instead */
103179
writeContractAsync: <
104180
const abi2 extends abi,
105181
name extends functionName extends ContractFunctionName<
@@ -158,76 +234,78 @@ export function createUseWriteContract<
158234
const result = useWriteContract(parameters)
159235
const configChainId = useChainId({ config })
160236
type Args = Parameters<wagmi_UseWriteContractReturnType['writeContract']>
161-
return {
162-
...(result as any),
163-
writeContract: useCallback(
164-
(...args: Args) => {
165-
const chainId = (() => {
166-
if (args[0].chainId) return args[0].chainId
167-
return configChainId
168-
})()
169-
const variables = {
170-
...(args[0] as any),
171-
address: chainId ? props.address?.[chainId] : undefined,
172-
...(props.functionName
173-
? { functionName: props.functionName }
174-
: {}),
175-
abi: props.abi,
176-
}
177-
result.writeContract(variables, args[1] as any)
178-
},
179-
[props, configChainId, result.writeContract],
180-
),
181-
writeContractAsync: useCallback(
182-
(...args: Args) => {
183-
const chainId = (() => {
184-
if (args[0].chainId) return args[0].chainId
185-
return configChainId
186-
})()
187-
const variables = {
188-
...(args[0] as any),
189-
address: chainId ? props.address?.[chainId] : undefined,
190-
...(props.functionName
191-
? { functionName: props.functionName }
192-
: {}),
193-
abi: props.abi,
194-
}
195-
return result.writeContractAsync(variables, args[1] as any)
196-
},
197-
[props, configChainId, result.writeContractAsync],
198-
),
199-
}
200-
}
201-
202-
return (parameters) => {
203-
const result = useWriteContract(parameters)
204-
type Args = Parameters<wagmi_UseWriteContractReturnType['writeContract']>
205-
return {
206-
...(result as any),
207-
writeContract: useCallback(
237+
const mutate = useCallback(
208238
(...args: Args) => {
239+
const chainId = (() => {
240+
if (args[0].chainId) return args[0].chainId
241+
return configChainId
242+
})()
209243
const variables = {
210244
...(args[0] as any),
211-
...(props.address ? { address: props.address } : {}),
245+
address: chainId ? props.address?.[chainId] : undefined,
212246
...(props.functionName ? { functionName: props.functionName } : {}),
213247
abi: props.abi,
214248
}
215249
result.writeContract(variables, args[1] as any)
216250
},
217-
[props, result.writeContract],
218-
),
219-
writeContractAsync: useCallback(
251+
[props, configChainId, result.writeContract],
252+
)
253+
const mutateAsync = useCallback(
220254
(...args: Args) => {
255+
const chainId = (() => {
256+
if (args[0].chainId) return args[0].chainId
257+
return configChainId
258+
})()
221259
const variables = {
222260
...(args[0] as any),
223-
...(props.address ? { address: props.address } : {}),
261+
address: chainId ? props.address?.[chainId] : undefined,
224262
...(props.functionName ? { functionName: props.functionName } : {}),
225263
abi: props.abi,
226264
}
227265
return result.writeContractAsync(variables, args[1] as any)
228266
},
229-
[props, result.writeContractAsync],
230-
),
267+
[props, configChainId, result.writeContractAsync],
268+
)
269+
return {
270+
...(result as any),
271+
mutate,
272+
mutateAsync,
273+
writeContract: mutate,
274+
writeContractAsync: mutateAsync,
275+
}
276+
}
277+
278+
return (parameters) => {
279+
const result = useWriteContract(parameters)
280+
type Args = Parameters<wagmi_UseWriteContractReturnType['writeContract']>
281+
const mutate = useCallback(
282+
(...args: Args) => {
283+
const variables = {
284+
...(args[0] as any),
285+
...(props.address ? { address: props.address } : {}),
286+
...(props.functionName ? { functionName: props.functionName } : {}),
287+
abi: props.abi,
288+
}
289+
result.mutate(variables, args[1] as any)
290+
},
291+
[props, result.mutate],
292+
)
293+
const mutateAsync = useCallback(
294+
(...args: Args) => {
295+
const variables = {
296+
...(args[0] as any),
297+
...(props.address ? { address: props.address } : {}),
298+
...(props.functionName ? { functionName: props.functionName } : {}),
299+
abi: props.abi,
300+
}
301+
return result.mutateAsync(variables, args[1] as any)
302+
},
303+
[props, result.mutateAsync],
304+
)
305+
return {
306+
...(result as any),
307+
writeContract: mutate,
308+
writeContractAsync: mutateAsync,
231309
}
232310
}
233311
}

packages/react/src/hooks/useCallsStatus.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test('default', async () => {
1414

1515
const { result } = await renderHook(() => useSendCalls())
1616

17-
result.current.sendCalls({
17+
result.current.mutate({
1818
calls: [
1919
{
2020
data: '0xdeadbeef',

0 commit comments

Comments
 (0)