Skip to content

Commit

Permalink
Docs for useReactiveVar.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Aug 20, 2020
1 parent 141794c commit 002e199
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions docs/source/local-state/managing-state-with-field-policies.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,14 @@ This `read` function returns the value of our reactive variable whenever `cartIt

Now, let's create a button component that enables the user to add a product to their cart:

```jsx{8}:title=AddToCartButton.js
```jsx{7}:title=AddToCartButton.js
import { cartItemsVar } from './cache';
// ... other imports

export function AddToCartButton({ productId }) {
const cartItems = cartItemsVar();
return (
<div class="add-to-cart-button">
<Button onClick={() => cartItemsVar([...cartItems, productId])}>
<Button onClick={() => cartItemsVar([...cartItemsVar(), productId])}>
Add to Cart
</Button>
</div>
Expand All @@ -167,8 +166,6 @@ On click, this button updates the value of `cartItemsVar` to append the button's

Here's a `Cart` component that uses the `GET_CART_ITEMS` query and therefore refreshes automatically whenever the value of `cartItemsVar` changes:

<ExpansionPanel title="Expand example">

```jsx:title=Cart.js
export const GET_CART_ITEMS = gql`
query GetCartItems {
Expand Down Expand Up @@ -199,7 +196,32 @@ export function Cart() {
}
```

</ExpansionPanel>
Alternatively, you can read directly from a reactive variable using the `useReactiveVar` hook introduced in Apollo Client 3.2.0:

```jsx:title=Cart.js
import { useReactiveVar } from '@apollo/client';

export function Cart() {
const cartItems = useReactiveVar(cartItemsVar);

This comment has been minimized.

Copy link
@KhangHLe

KhangHLe Jun 28, 2021

Do you need to import cartItemsVar or does useReactiveVar read from the global state?

This comment has been minimized.

Copy link
@benjamn

benjamn Jun 28, 2021

Author Member

Yes, you do need to import it from somewhere, like we do above with

import { cartItemsVar } from "./cache"

or create it locally with

import { makeVar, useReactiveVar } from "@apollo/client";
export const cartItemsVar = makeVar([]);

export function Cart() {
  const cartItems = useReactiveVar(cartItemsVar);
  // ...

return (
<div class="cart">
<Header>My Cart</Header>
{cartItems.length === 0 ? (
<p>No items in your cart</p>
) : (
<Fragment>
{cartItems.map(productId => (
<CartItem key={productId} />
))}
</Fragment>
)}
</div>
);
}
```

As in the earlier `useQuery` example, whenever the `cartItemsVar` variable is updated, any currently-mounted `Cart` components will rerender. Calling `cartItemsVar()` without `useReactiveVar` will not capture this dependency, so future variable updates will not rerender the component. Both of these approaches are useful in different situations.

### Storing local state in the cache

Expand Down

0 comments on commit 002e199

Please sign in to comment.