Skip to content

Commit

Permalink
- update cache-management as per lifecycle api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrugsy committed May 13, 2021
1 parent 6c7e403 commit 0061965
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions docs/api/rtk-query/created-api/cache-management.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const updateQueryResult = (
interface PatchCollection {
patches: Patch[];
inversePatches: Patch[];
undo: () => void;
}
```

Expand All @@ -37,9 +38,9 @@ A Redux thunk action creator that, when dispatched, creates and applies a set of

The thunk action creator accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), any relevant query arguments, and a callback function. The callback receives an Immer-wrapped `draft` of the current state, and may modify the draft to match the expected results after the mutation completes successfully.

The thunk returns an object containing `{patches: Patch[], inversePatches: Patch[]}`, generated using Immer's [`produceWithPatches` method](https://immerjs.github.io/immer/patches).
The thunk returns an object containing `{patches: Patch[], inversePatches: Patch[], undo: () => void}`. The `patches` and `inversePatches` are generated using Immer's [`produceWithPatches` method](https://immerjs.github.io/immer/patches).

This is typically used as the first step in implementing optimistic updates. The generated `inversePatches` can be passed between the `onStart` and `onError` callbacks in a mutation definition by assigning the `inversePatches` array to the provided `context` object, and then the updates can be reverted by calling `patchQueryResult(endpointName, args, inversePatches)`.
This is typically used as the first step in implementing optimistic updates. The generated `inversePatches` can be used to revert the updates by calling `dispatch(patchQueryResult(endpointName, args, inversePatches))`. Alternatively, the `undo` method can be called directly to achieve the same effect.

### `patchQueryResult`

Expand All @@ -59,7 +60,7 @@ A Redux thunk that applies a JSON diff/patch array to the cached data for a give

The thunk accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), any relevant query arguments, and a JSON diff/patch array as produced by Immer's `produceWithPatches`.

This is typically used as the second step in implementing optimistic updates. If a request fails, the optimistically-applied changes can be reverted by dispatching `patchQueryResult` with the `inversePatches` that were generated by `updateQueryResult` earlier.
This is typically used as the second step in implementing optimistic updates. If a request fails, the optimistically-applied changes can be reverted by dispatching `patchQueryResult` with the `inversePatches` that were generated by `updateQueryResult` earlier, or by calling the `undo` method returned from dispatching `updateQueryResult`.

### `prefetch`

Expand Down

0 comments on commit 0061965

Please sign in to comment.