Skip to content

Commit

Permalink
Remove troubleshooting section, update Modifying section with more co…
Browse files Browse the repository at this point in the history
…ncise example.
  • Loading branch information
larrymyers authored and bignimbus committed Nov 21, 2022
1 parent b888e66 commit a28a84d
Showing 1 changed file with 7 additions and 55 deletions.
62 changes: 7 additions & 55 deletions docs/source/local-state/reactive-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,22 @@ To modify the value of your reactive variable, call the function returned by `ma

```js
const cartItemsVar = makeVar([]);
const cartItemIds = [100, 101, 102];

cartItemsVar([100, 101, 102]);
cartItemsVar(cartItemIds);

// Output: [100, 101, 102]
console.log(cartItemsVar());

cartItemsVar([456]);
// Don't mutate an existing object or array.
// cartItemIds.push(456) will not trigger an update.
// Instead, pass a new copy:
cartItemsVar([...cartItemIds, 456]);

// Output: [456]
// Output: [100, 101, 102, 456]
console.log(cartItemsVar());
```

In the above example note that a new array is passed in every time to the reactive variable. Currently `useReactiveVar` only triggers component updates when new values are passed to the reactive variable. It uses strict equality checks (the `===` operator) to determine if the value has changed. Make sure you are always passing new values when performing an update.

This will *not* trigger an update:

```ts
const cartItemsVar = makeVar([1, 2]);
const cartItems = cartItemsVar();

cartItems.push(3);
cartItemsVarVar(cartItems);
```

This will trigger an update:

```ts
const cartItemsVar = makeVar([1, 2]);
const cartItems = cartItemsVar();

cartItemsVar([...cartItems, 3]); // spread operator to use existing values
cartItemsVar(cartItems.concat(4)); // Array.concat creates a new array
```

## Reacting

As their name suggests, reactive variables can trigger reactive changes in your application. Whenever you modify the value of a reactive variable, queries that depend on that variable refresh, and your application's UI updates accordingly.
Expand All @@ -97,36 +79,6 @@ export function Cart() {
// ...
```
## Troubleshooting
### Updates Outside of Components
If you are making updates to a reactive variable outside of a React component and your components that use `useReactiveVar` are not updating, try deferring the update to the next tick of the event loop.
Here's an example that tracks GraphQL errors using the [onError Link](../api/link/apollo-link-error.md).
```ts
const errorsVar = makeVar<Error[]>([]);

const errorLink = onError((error: ErrorResponse) => {
const errors = errorsVar();

// making a copy of the current array value will trigger an update
const nextErrors = [...errors];

if (error.graphQLErrors) {
error.graphQLErrors.forEach((e) => nextErrors.push(e));
}

if (error.networkError) {
nextErrors.push(error.networkError);
}

// deferring the update to next tick will allow the update to reliably trigger a new component render
setTimeout(() => errorsVar(nextErrors));
});
```
## Example application
[This example to-do list application](https://github.com/apollographql/ac3-state-management-examples/tree/master/apollo-local-state) uses reactive variables to track both the current list of to-do items and the filter that determines which items to display. See [`cache.tsx`](https://github.com/apollographql/ac3-state-management-examples/blob/master/apollo-local-state/src/cache.tsx) in particular.

0 comments on commit a28a84d

Please sign in to comment.