Skip to content

Commit

Permalink
Add note discussing immutability to update* docs (#10522)
Browse files Browse the repository at this point in the history
* Add note discussing immutability to `update*` docs

* Apply suggestions from code review

Co-authored-by: Jerel Miller <[email protected]>

---------

Co-authored-by: Jerel Miller <[email protected]>
Co-authored-by: Alessia Bellisario <[email protected]>
  • Loading branch information
3 people authored Feb 6, 2023
1 parent 015a1ff commit 7ff4b93
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/source/api/cache/InMemoryCache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ The update function of `updateQuery` takes a query's current cached value and re

The returned value is automatically passed to [`writeQuery`](#writequery), which modifies the cached data.

Please note the `update` function has to calculate the new value in an immutable way. You can read more about immutable updates in the [React documentation](https://beta.reactjs.org/learn/updating-objects-in-state).

### Signature

```ts title="src/cache/core/cache.ts"
Expand Down Expand Up @@ -892,6 +894,8 @@ The update function of `updateFragment` takes a fragment's current cached value

The returned value is automatically passed to [`writeFragment`](#writefragment), which modifies the cached data.

Please note the `update` function has to calculate the new value in an immutable way. You can read more about immutable updates in the [React documentation](https://beta.reactjs.org/learn/updating-objects-in-state).

### Signature

```ts title="src/cache/core/cache.ts"
Expand Down
2 changes: 2 additions & 0 deletions docs/source/caching/cache-interaction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ Each of these methods takes two parameters:

After either method fetches data from the cache, it calls its update function and passes it the cached `data`. The update function can then return a value to _replace_ that `data` in the cache. In the example above, every cached `Todo` object has its `completed` field set to `true` (and other fields remain unchanged).

Please note that the replacement value has to be calculated in an immutable way. You can read more about immutable updates in [the React documentation](https://beta.reactjs.org/learn/updating-objects-in-state).

If the update function shouldn't make _any_ changes to the cached data, it can return `undefined`.

The update function's return value is passed to either `writeQuery` or `writeFragment`, which modifies the cached data.
Expand Down

0 comments on commit 7ff4b93

Please sign in to comment.