Reactive variables

State containers integrated into Apollo Client's reactivity model


New in Apollo Client 3, reactive variables are a useful mechanism for representing local state outside of the Apollo Client cache. Because they're separate from the cache, reactive variables can store data of any type and structure, and you can interact with them anywhere in your application without using GraphQL syntax.

Most importantly, modifying a reactive variable triggers an update of every active query that depends on that variable. Additionally, this updates the React state for components that use the useReactiveVar React hook.

A query "depends on" a reactive variable if any of the query's requested fields defines a read function that reads the variable's value.

Creating

Create a reactive variable with the makeVar method, like so:

JavaScript
1import { makeVar } from '@apollo/client';
2
3const cartItemsVar = makeVar([]);

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.

Reading

To read the value of your reactive variable, call the function returned by makeVar with zero arguments:

JavaScript
1const cartItemsVar = makeVar([]);
2
3// Output: []
4console.log(cartItemsVar());

Modifying

To modify the value of your reactive variable, call the function returned by makeVar with one argument (the variable's new value):

JavaScript
1const cartItemsVar = makeVar([]);
2const cartItemIds = [100, 101, 102];
3
4cartItemsVar(cartItemIds);
5
6// Output: [100, 101, 102]
7console.log(cartItemsVar());
8
9// Don't mutate an existing object or array.
10// cartItemIds.push(456) will not trigger an update.
11// Instead, pass a new copy:
12cartItemsVar([...cartItemIds, 456]);
13
14// Output: [100, 101, 102, 456]
15console.log(cartItemsVar());

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.

With the useReactiveVar hook, React components can also include reactive variable values in their state directly, without wrapping them in a query.

For more information, see 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.

JavaScript
1import { makeVar, useReactiveVar } from "@apollo/client";
2export const cartItemsVar = makeVar([]);
3
4export function Cart() {
5  const cartItems = useReactiveVar(cartItemsVar);
6  // ...

Example application

This example to-do list application 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 in particular.

Feedback

Edit on GitHub

Forums