Skip to main content
TanStack Store provides Preact support through the @tanstack/preact-store package, which implements a lightweight version of React’s useSyncExternalStore for optimal performance.

Installation

Install the Preact adapter package:
The @tanstack/preact-store package re-exports everything from @tanstack/store, so you only need to install the Preact package.

Basic Usage

The primary way to use TanStack Store in Preact is through the useStore hook:

The useStore Hook

The useStore hook subscribes your component to store updates and automatically triggers re-renders when selected state changes.

Signature

Parameters

  • store - The store instance to subscribe to
  • selector - A function that selects which part of the state you need
  • options.equal - (Optional) A custom equality function. Defaults to shallow

Implementation Details

The Preact adapter includes a custom implementation of useSyncExternalStore that:
  • Avoids importing preact/compat (no side effects)
  • Uses useLayoutEffect and useEffect for optimal timing
  • Provides the same guarantees as React’s useSyncExternalStore

Selector Optimization

The selector function allows you to subscribe to only the parts of state you need:

Custom Equality Functions

For complex state selections, you can provide a custom equality function:

Shallow Equality

The package exports a shallow utility for shallow object comparison (this is the default):
The shallow function works with:
  • Plain objects
  • Maps
  • Sets
  • Dates
  • Symbol keys

Complete Example: Todo App

Here’s a complete example demonstrating store creation, updates, and Preact integration:

Preact-Specific Considerations

No preact/compat Required

Unlike some libraries, @tanstack/preact-store doesn’t require preact/compat. It includes a custom implementation of useSyncExternalStore specifically for Preact.

Lightweight Implementation

The adapter is highly optimized:
  • Small bundle size
  • No external dependencies (except Preact itself)
  • Efficient re-render behavior

Server-Side Rendering (SSR)

TanStack Store works with Preact SSR. Create stores outside of components and initialize them with server data:

Performance Tips

  1. Use selectors wisely: Select only what you need to minimize re-renders
  2. Leverage shallow equality: The default works well for most cases
  3. Memoize selectors: For expensive computations, consider memoizing selector functions
  4. Create multiple stores: Instead of one large store, create multiple smaller stores

Derived Stores

You can create derived stores that automatically update based on other stores:

Migration from React

If you’re migrating from React, the API is identical:
The only difference is the import path. Your component code can remain the same.

Comparison with Preact Signals

Preact has built-in signals. Here’s when to use TanStack Store: Use TanStack Store when:
  • You want a consistent API across multiple frameworks
  • You need advanced features like derived stores
  • You’re migrating from React
  • You want structured state management
Use Preact Signals when:
  • You want the lightest possible solution
  • You’re building a Preact-only app
  • You prefer a more reactive programming style
You can also use both together!

API Reference

For detailed API documentation, see:
  • See the API reference for detailed documentation:

TypeScript Support

The Preact adapter provides full TypeScript support with automatic type inference: