Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Zustand · Pipe

partialize

partialize is a persist option that chooses which store fields are written to storage. Return a smaller object to keep selected state across reloads while excluding transient fields.

How it works

Within persist options, partialize receives the complete store state and returns the object that should be written to storage. Returning a smaller object allows selected fields to survive reloads while transient fields and actions remain runtime-only; omitting the option uses the entire state by default.

Syntax

partialize: (state: Object) => Object

Parameters

NameTypeDescription
stateObjectThe complete store state; the callback uses it to construct the persisted object.

Returns

An object containing the subset of state fields to store; by default, the original state object is returned.

Key points

  • partialize selects the portion of a Zustand store that persist writes to storage.
  • Return an object containing only the fields that should survive persistence.
  • The callback can project one field into a new persisted object.

Common mistakes

  • Without partialize, the default stores the complete state object.

Example: Persist selected state

import { create } from 'zustand'
import { persist } from 'zustand/middleware'

type Store = {
  token: string
  theme: 'light' | 'dark'
  setToken: (token: string) => void
  setTheme: (theme: Store['theme']) => void
}

export const useStore = create<Store>()(
  persist(
    (set) => ({
      token: '',
      theme: 'light',
      setToken: (token) => set({ token }),
      setTheme: (theme) => set({ theme }),
    }),
    {
      name: 'app-storage',
      partialize: (state) => ({ theme: state.theme }),
    },
  ),
)

Persists only the theme while leaving the token and action functions out of stored data.

When to use it

Selective persistence

Persist user preferences while keeping temporary UI state in memory.

Exclude sensitive or ephemeral data

Avoid writing credentials, caches, or other transient fields to storage.

Reduce persisted state

Store only the fields needed to reconstruct a feature after reload.

Interview questions

QWhat does partialize do?

partialize is a persist option typed as a function from an object state to an object. Its return value determines which state fields are stored, so returning { theme: state.theme } persists only that field.

Follow-up: What happens when the option is omitted? The identity function is used, so the complete state is selected for persistence.

QHow do you persist only part of a store?

Use partialize to return a new object containing only the fields that should be persisted. The other fields remain available in the live Zustand store but are excluded from the persisted representation.

Follow-up: How could you exclude several named fields? Build the returned object by filtering the state's entries, as shown in the documentation.

QWhere is partialize configured?

partialize belongs in the options object passed to persist, alongside settings such as the storage name. It receives the current state and returns the persisted subset.

Follow-up: What is the type of the option? (state: Object) => Object, with the identity function as its default.

  • persist
  • storage
  • hydrate
  • createJSONStorage

Source: Persisting store data - Zustand Docs. Checked 2026-10-15.