Skip to content

Commit

Permalink
Use reactive var hook (apollographql#9906)
Browse files Browse the repository at this point in the history
* emptye

* adding content on useReactiveVar

* added to react-hooks section

* resolved feedback

* fixed function signature
  • Loading branch information
jpvajda authored Jul 19, 2022
1 parent fa3c3c9 commit cc3ab27
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/source/api/react/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,27 @@ function useApolloClient(): ApolloClient<object> {}
| Param | Type | Description |
| ---------------------- | -------------------------- | ---------------------------------------------------------- |
| Apollo Client instance | ApolloClient&lt;object&gt; | The `ApolloClient` instance being used by the application. |


## useReactiveVar

The `useReactiveVar` hook can be used to read from a reactive variable in a way that allows the React component to re-render if/when the variable is next updated.

Previously, the only way for a reactive variable to trigger a React component rerender was through the use of `useQuery`. Now you don't have to be using `useQuery` to benefit from the reactivity that `ReactiveVar<T>` provides.

### Example

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

export function Cart() {
const cartItems = useReactiveVar(cartItemsVar);
// ...
```
### Function Signature
```tsx
function useReactiveVar<T>(rv: ReactiveVar<T>): T {}
```
19 changes: 17 additions & 2 deletions docs/source/local-state/reactive-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { makeVar } from '@apollo/client';
const cartItemsVar = makeVar([]);
```

This code creates a reactive variable with an empty array as its initial value.
This code creates a reactive variable with an empty array as its initial value.

> **Important:** The return value of `makeVar` is a _function_ that you call to read or modify your reactive variable's value.
Expand Down Expand Up @@ -60,6 +60,21 @@ With the `useReactiveVar` hook, React components can also include reactive varia

For more information, see [Storing local state in reactive variables](./managing-state-with-field-policies/#storing-local-state-in-reactive-variables).

### useReactiveVar hook

The `useReactiveVar` hook can be used to read from a reactive variable in a way that allows the React component to re-render if/when the variable is next updated.

Previously, the only way for a reactive variable to trigger a React component re-render was through the use of `useQuery`. Now you don't have to be using `useQuery` to benefit from the reactivity that `ReactiveVar<T>` provides.

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

export function Cart() {
const cartItems = useReactiveVar(cartItemsVar);
// ...
```
## 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.
[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 cc3ab27

Please sign in to comment.