Angular Query 提供了两种在突变完成之前乐观更新 UI 的方法。你可以使用 onMutate 选项直接更新缓存,或者利用返回的 variables 从 injectMutation 结果中更新 UI。
这是更简单的变体,因为它不直接与缓存交互。
addTodo = injectMutation(() => ({
mutationFn: (newTodo: string) => axios.post('/api/data', { text: newTodo }),
// make sure to _return_ the Promise from the query invalidation
// so that the mutation stays in `pending` state until the refetch is finished
onSettled: async () => {
return await queryClient.invalidateQueries({ queryKey: ['todos'] })
},
}))
addTodo = injectMutation(() => ({
mutationFn: (newTodo: string) => axios.post('/api/data', { text: newTodo }),
// make sure to _return_ the Promise from the query invalidation
// so that the mutation stays in `pending` state until the refetch is finished
onSettled: async () => {
return await queryClient.invalidateQueries({ queryKey: ['todos'] })
},
}))
然后你将可以访问包含已添加 todo 的 addTodo.variables。在你的 UI 列表中,查询被渲染的位置,你可以在突变 isPending 时向列表追加另一个项目
@Component({
template: `
@for (todo of todos.data(); track todo.id) {
<li>{{ todo.title }}</li>
}
@if (addTodo.isPending()) {
<li style="opacity: 0.5">{{ addTodo.variables() }}</li>
}
`,
})
class TodosComponent {}
@Component({
template: `
@for (todo of todos.data(); track todo.id) {
<li>{{ todo.title }}</li>
}
@if (addTodo.isPending()) {
<li style="opacity: 0.5">{{ addTodo.variables() }}</li>
}
`,
})
class TodosComponent {}
只要突变处于 pending 状态,我们就会渲染一个具有不同 opacity 的临时项目。一旦完成,该项目将自动不再渲染。考虑到重新获取成功,我们应该在列表中看到该项目作为“正常项目”。
如果突变出错,该项目也会消失。但是,如果需要,我们可以通过检查突变的 isError 状态来继续显示它。variables 在突变出错时不会被清除,因此我们仍然可以访问它们,甚至可以显示重试按钮
@Component({
template: `
@if (addTodo.isError()) {
<li style="color: red">
{{ addTodo.variables() }}
<button (click)="addTodo.mutate(addTodo.variables())">Retry</button>
</li>
}
`,
})
class TodosComponent {}
@Component({
template: `
@if (addTodo.isError()) {
<li style="color: red">
{{ addTodo.variables() }}
<button (click)="addTodo.mutate(addTodo.variables())">Retry</button>
</li>
}
`,
})
class TodosComponent {}
如果突变和查询在同一个组件中,这种方法效果很好。但是,你也可以通过专用的 injectMutationState 函数访问其他组件中的所有突变。最好与 mutationKey 结合使用
// somewhere in your app
addTodo = injectMutation(() => ({
mutationFn: (newTodo: string) => axios.post('/api/data', { text: newTodo }),
onSettled: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
mutationKey: ['addTodo'],
}))
// access variables somewhere else
mutationState = injectMutationState<string>(() => ({
filters: { mutationKey: ['addTodo'], status: 'pending' },
select: (mutation) => mutation.state.variables,
}))
// somewhere in your app
addTodo = injectMutation(() => ({
mutationFn: (newTodo: string) => axios.post('/api/data', { text: newTodo }),
onSettled: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
mutationKey: ['addTodo'],
}))
// access variables somewhere else
mutationState = injectMutationState<string>(() => ({
filters: { mutationKey: ['addTodo'], status: 'pending' },
select: (mutation) => mutation.state.variables,
}))
variables 将是一个 Array,因为可能同时运行多个突变。如果我们需要项目的唯一键,我们还可以选择 mutation.state.submittedAt。这将使显示并发乐观更新变得轻而易举。
当你在执行突变之前乐观地更新状态时,突变有可能失败。在大多数这些失败情况下,你可以只需为乐观查询触发重新获取,以将它们恢复到其真实的服务器状态。但在某些情况下,重新获取可能无法正常工作,并且突变错误可能代表某种类型的服务器问题,使得无法重新获取。在这种情况下,你可以选择回滚你的更新。
为此,injectMutation 的 onMutate 处理程序选项允许你返回一个值,该值稍后将作为最后一个参数传递给 onError 和 onSettled 处理程序。在大多数情况下,传递回滚函数是最有用的。
queryClient = inject(QueryClient)
updateTodo = injectMutation(() => ({
mutationFn: updateTodo,
// When mutate is called:
onMutate: async (newTodo) => {
// Cancel any outgoing refetches
// (so they don't overwrite our optimistic update)
await this.queryClient.cancelQueries({ queryKey: ['todos'] })
// Snapshot the previous value
const previousTodos = client.getQueryData(['todos'])
// Optimistically update to the new value
this.queryClient.setQueryData(['todos'], (old) => [...old, newTodo])
// Return a context object with the snapshotted value
return { previousTodos }
},
// If the mutation fails,
// use the context returned from onMutate to roll back
onError: (err, newTodo, context) => {
client.setQueryData(['todos'], context.previousTodos)
},
// Always refetch after error or success:
onSettled: () => {
this.queryClient.invalidateQueries({ queryKey: ['todos'] })
},
}))
queryClient = inject(QueryClient)
updateTodo = injectMutation(() => ({
mutationFn: updateTodo,
// When mutate is called:
onMutate: async (newTodo) => {
// Cancel any outgoing refetches
// (so they don't overwrite our optimistic update)
await this.queryClient.cancelQueries({ queryKey: ['todos'] })
// Snapshot the previous value
const previousTodos = client.getQueryData(['todos'])
// Optimistically update to the new value
this.queryClient.setQueryData(['todos'], (old) => [...old, newTodo])
// Return a context object with the snapshotted value
return { previousTodos }
},
// If the mutation fails,
// use the context returned from onMutate to roll back
onError: (err, newTodo, context) => {
client.setQueryData(['todos'], context.previousTodos)
},
// Always refetch after error or success:
onSettled: () => {
this.queryClient.invalidateQueries({ queryKey: ['todos'] })
},
}))
queryClient = inject(QueryClient)
updateTodo = injectMutation(() => ({
mutationFn: updateTodo,
// When mutate is called:
onMutate: async (newTodo) => {
// Cancel any outgoing refetches
// (so they don't overwrite our optimistic update)
await this.queryClient.cancelQueries({ queryKey: ['todos', newTodo.id] })
// Snapshot the previous value
const previousTodo = this.queryClient.getQueryData(['todos', newTodo.id])
// Optimistically update to the new value
this.queryClient.setQueryData(['todos', newTodo.id], newTodo)
// Return a context with the previous and new todo
return { previousTodo, newTodo }
},
// If the mutation fails, use the context we returned above
onError: (err, newTodo, context) => {
this.queryClient.setQueryData(
['todos', context.newTodo.id],
context.previousTodo,
)
},
// Always refetch after error or success:
onSettled: (newTodo) => {
this.queryClient.invalidateQueries({ queryKey: ['todos', newTodo.id] })
},
}))
queryClient = inject(QueryClient)
updateTodo = injectMutation(() => ({
mutationFn: updateTodo,
// When mutate is called:
onMutate: async (newTodo) => {
// Cancel any outgoing refetches
// (so they don't overwrite our optimistic update)
await this.queryClient.cancelQueries({ queryKey: ['todos', newTodo.id] })
// Snapshot the previous value
const previousTodo = this.queryClient.getQueryData(['todos', newTodo.id])
// Optimistically update to the new value
this.queryClient.setQueryData(['todos', newTodo.id], newTodo)
// Return a context with the previous and new todo
return { previousTodo, newTodo }
},
// If the mutation fails, use the context we returned above
onError: (err, newTodo, context) => {
this.queryClient.setQueryData(
['todos', context.newTodo.id],
context.previousTodo,
)
},
// Always refetch after error or success:
onSettled: (newTodo) => {
this.queryClient.invalidateQueries({ queryKey: ['todos', newTodo.id] })
},
}))
如果你愿意,你也可以使用 onSettled 函数来代替单独的 onError 和 onSuccess 处理程序
injectMutation({
mutationFn: updateTodo,
// ...
onSettled: (newTodo, error, variables, context) => {
if (error) {
// do something
}
},
})
injectMutation({
mutationFn: updateTodo,
// ...
onSettled: (newTodo, error, variables, context) => {
if (error) {
// do something
}
},
})
如果你只有一个地方需要显示乐观结果,那么使用 variables 并直接更新 UI 是一种需要较少代码且通常更容易理解的方法。例如,你根本不需要处理回滚。
但是,如果你的屏幕上有多个地方需要知道更新,直接操作缓存将自动为你处理此事。