# Transient Updates

> Handle high-frequency updates like scroll or mouse tracking without crushing React's render cycle.

Subject: State Management | Lesson 8 of 22 | 6 min read
Interactive lesson: https://codewithanimation.in/state-management/zustand-transient-updates
Previous: [The Slices Pattern](https://codewithanimation.in/state-management/zustand-slices-pattern.md) | Next: [Middleware & Persist](https://codewithanimation.in/state-management/zustand-middleware-and-persist.md)

## What are Transient Updates?

If you try to track the user's mouse coordinates by storing them in React state (`useState`) or a Context Provider, your entire component tree will re-render 60 times per second. Your app will grind to a halt.

Transient updates allow you to read and respond to Zustand state changes *without* triggering a React render cycle. You do this by subscribing to the store directly.

## Bypassing the React Render Cycle

React is fast, but it's not meant to handle 60fps continuous data streams if that data doesn't structurally change the DOM.

By using Zustand's `subscribe` method, you can listen for state changes and update a DOM node directly using a React `ref`. This bypasses React's virtual DOM diffing entirely, resulting in butter-smooth performance.

## Using `store.subscribe`

Every Zustand store exposes a `.subscribe()` method. You can set this up inside a `useEffect`.

```tsx
useEffect(() => {
  const unsubscribe = useStore.subscribe((state) => {
    if (ref.current) {
      // Update the DOM directly!
      ref.current.innerText = `X: ${state.x}, Y: ${state.y}`;
    }
  });
  return unsubscribe; // Cleanup on unmount
}, []);
```

Because we aren't calling `useStore()` as a hook in the component body, this component will *never* re-render when `x` or `y` changes, but the text on the screen will still update.

## Common mistake: When to drop out of React

Transient updates are powerful, but they break the declarative nature of React. You are essentially doing manual DOM manipulation (like the old jQuery days). Use this technique strictly for performance-critical, high-frequency updates (scroll tracking, drag-and-drop coordinates, WebGL interactions) and stick to standard hooks for everything else.

## Tip: Reading state without subscribing

If you just need to read the current state once (e.g., inside an event handler) without subscribing to future updates, you can use `useStore.getState()`. This gets the latest state instantly without causing re-renders.

## Try it: 60FPS Rendering

Watch how a high-frequency "mouse tracking" signal updates the DOM instantly via `subscribe` and a `ref`, all while the React render count remains perfectly at zero.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/zustand-transient-updates

## Check yourself

### Question 1

Why would you use useStore.subscribe() instead of the useStore() hook?

- A. To listen for state changes and update a ref directly, avoiding a React re-render.
- B. Because useStore() is only for synchronous updates.
- C. Because subscribe() allows you to mutate the state.

Correct answer: A

subscribe() allows you to react to state changes completely outside of React's render cycle, which is essential for high-frequency events like mouse tracking.

- Why not B: useStore() handles async updates fine, the difference is about triggering re-renders.
- Why not C: subscribe() is for reading/listening, not mutating. You mutate with set().

## Remember this

- High-frequency state updates (60fps) will destroy React performance if they trigger re-renders.
- Use `useStore.subscribe()` inside a `useEffect` to listen for changes silently.
- Update DOM nodes directly via a `ref` inside the subscribe callback.
- Use `useStore.getState()` to read the store once without subscribing.
