@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 theuseStore hook:
The useStore Hook
TheuseStore hook subscribes your component to store updates and automatically triggers re-renders when selected state changes.
Signature
Parameters
store- The store instance to subscribe toselector- A function that selects which part of the state you needoptions.equal- (Optional) A custom equality function. Defaults toshallow
Implementation Details
The Preact adapter includes a custom implementation ofuseSyncExternalStore that:
- Avoids importing
preact/compat(no side effects) - Uses
useLayoutEffectanduseEffectfor 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 ashallow utility for shallow object comparison (this is the default):
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
- Use selectors wisely: Select only what you need to minimize re-renders
- Leverage shallow equality: The default works well for most cases
- Memoize selectors: For expensive computations, consider memoizing selector functions
- 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: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
- You want the lightest possible solution
- You’re building a Preact-only app
- You prefer a more reactive programming style
API Reference
For detailed API documentation, see:- See the API reference for detailed documentation: