Open History to see the last commit
1---
2id: usePacedMutations
3title: usePacedMutations
4---
5
6```ts
7function usePacedMutations<TVariables, T>(config): (variables) => Transaction<T>;
8```
9
10Defined in: [usePacedMutations.ts:93](https://github.com/TanStack/db/blob/main/packages/react-db/src/usePacedMutations.ts#L93)
11
12React hook for managing paced mutations with timing strategies.
13
14Provides optimistic mutations with pluggable strategies like debouncing,
15queuing, or throttling. The optimistic updates are applied immediately via
16`onMutate`, and the actual persistence is controlled by the strategy.
17
18## Type Parameters
19
20### TVariables
21
22`TVariables` = `unknown`
23
24### T
25
26`T` *extends* `object` = `Record`\<`string`, `unknown`\>
27
28## Parameters
29
30### config
31
32`PacedMutationsConfig`\<`TVariables`, `T`\>
33
34Configuration including onMutate, mutationFn and strategy
35
36## Returns
37
38A mutate function that accepts variables and returns Transaction objects
39
40```ts
41(variables): Transaction<T>;
42```
43
44### Parameters
45
46#### variables
47
48`TVariables`
49
50### Returns
51
52`Transaction`\<`T`\>
53
54## Examples
55
56```tsx
57// Debounced auto-save
58function AutoSaveForm({ formId }: { formId: string }) {
59 const mutate = usePacedMutations<string>({
60 onMutate: (value) => {
61 // Apply optimistic update immediately
62 formCollection.update(formId, draft => {
63 draft.content = value
64 })
65 },
66 mutationFn: async ({ transaction }) => {
67 await api.save(transaction.mutations)
68 },
69 strategy: debounceStrategy({ wait: 500 })
70 })
71
72 const handleChange = async (value: string) => {
73 const tx = mutate(value)
74
75 // Optional: await persistence or handle errors
76 try {
77 await tx.isPersisted.promise
78 console.log('Saved!')
79 } catch (error) {
80 console.error('Save failed:', error)
81 }
82 }
83
84 return <textarea onChange={e => handleChange(e.target.value)} />
85}
86```
87
88```tsx
89// Throttled slider updates
90function VolumeSlider() {
91 const mutate = usePacedMutations<number>({
92 onMutate: (volume) => {
93 settingsCollection.update('volume', draft => {
94 draft.value = volume
95 })
96 },
97 mutationFn: async ({ transaction }) => {
98 await api.updateVolume(transaction.mutations)
99 },
100 strategy: throttleStrategy({ wait: 200 })
101 })
102
103 return <input type="range" onChange={e => mutate(+e.target.value)} />
104}
105```
106
107```tsx
108// Debounce with leading/trailing for color picker (persist first + final only)
109function ColorPicker() {
110 const mutate = usePacedMutations<string>({
111 onMutate: (color) => {
112 themeCollection.update('primary', draft => {
113 draft.color = color
114 })
115 },
116 mutationFn: async ({ transaction }) => {
117 await api.updateTheme(transaction.mutations)
118 },
119 strategy: debounceStrategy({ wait: 0, leading: true, trailing: true })
120 })
121
122 return (
123 <input
124 type="color"
125 onChange={e => mutate(e.target.value)}
126 />
127 )
128}
129```