-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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 { | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
benjamn
Author
Member
|
||
|
||
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 | ||
|
||
|
Do you need to import cartItemsVar or does useReactiveVar read from the global state?