Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
leptos-store
Enterprise-grade, type-enforced state management for Leptos
Overview
leptos-store provides a structured, SSR-safe state management architecture for Leptos, inspired by Vuex and Pinia, translated into idiomatic Rust.
Leptos provides excellent low-level primitives (signals, context, resources), but intentionally does not define a canonical, scalable state architecture. At scale, this absence can create challenges for large teams, enterprise governance, long-lived applications, SSR safety, and state auditing unless additional architectural patterns are introduced.
leptos-store exists to solve structure, not reactivity.
Features
- 🏗️ Global, namespaced stores - Clear domain boundaries
- 🔒 Predictable mutation flow - Only mutators can write state
- 🌐 First-class SSR support - Works seamlessly with server-side rendering
- 💧 SSR Hydration - Automatic state serialization and hydration between server and client
- ⚡ Async-safe actions - Built-in support for async operations
- 🔧 Compile-time enforcement - Catch errors at compile time, not runtime
- 📦 Zero magic - No hidden executors or runtime reflection
Installation
Add to your Cargo.toml:
[]
= "0.1"
= "0.8"
Feature Flags
| Feature | Default | Description |
|---|---|---|
ssr |
✅ Yes | Server-side rendering support |
hydrate |
❌ No | SSR hydration with automatic state serialization and transfer |
csr |
❌ No | Client-side rendering only (no SSR) |
Basic Usage (SSR without Hydration)
The ssr feature is enabled by default. For basic SSR without state hydration:
[]
= "0.1"
Full SSR with Hydration (Recommended for Production)
For full SSR applications where state needs to transfer from server to client, enable the hydrate feature. This requires different features for server and client builds:
[]
= { = "0.1", = false }
[]
= ["leptos-store/ssr", "leptos/ssr"]
= ["leptos-store/hydrate", "leptos/hydrate"]
The hydrate feature enables:
HydratableStoretrait for state serializationprovide_hydrated_store()- Server-side state embeddinguse_hydrated_store()- Client-side state recovery- Automatic JSON serialization via
serde
Client-Side Only
For SPAs without server rendering:
[]
= { = "0.1", = false, = ["csr"] }
Quick Start
Define Your Store
use *;
use *;
// Define your state
// Define your store
Use in Components
Using the store! Macro
For less boilerplate, use the declarative macro:
use store;
store!
Note: Use
this(or any identifier) instead ofselfin getter/mutator bodies due to Rust 2024 macro hygiene rules. The macro providesthis.read()for getters andthis.mutate()for mutators.
Available Macros
| Macro | Purpose | Feature |
|---|---|---|
define_state! |
Define state structs with default values | - |
define_hydratable_state! |
Define state with serde derives for hydration | hydrate |
define_action! |
Define synchronous action structs | - |
define_async_action! |
Define async action structs with result types | - |
impl_store! |
Implement Store trait for an existing type | - |
impl_hydratable_store! |
Implement HydratableStore trait | hydrate |
store! |
Complete store definition in one macro | - |
define_state! - State with Defaults
use define_state;
define_state!
let user = default;
assert_eq!;
assert!;
define_action! - Synchronous Actions
use define_action;
define_action!
let action = new;
define_async_action! - Async Actions with Error Types
use define_async_action;
// Define your error type
// Define the async action
define_async_action!
let action = new;
// Helper methods for documentation
assert!;
assert_eq!;
assert_eq!;
impl_store! - Quick Store Trait Implementation
use *;
use ;
// One-liner to implement Store trait
impl_store!;
Conceptual Model
Each store is a domain module composed of:
| Layer | Description | Can Write State | Async | Side Effects |
|---|---|---|---|---|
| State | Read-only externally | N/A | ❌ | ❌ |
| Getters | Derived, read-only | ❌ | ❌ | ❌ |
| Mutators | Pure, synchronous writes | ✅ | ❌ | ❌ |
| Actions | Sync orchestration | ❌ | ❌ | ✅ |
| Async Actions | Async orchestration | ❌ | ✅ | ✅ |
Only mutators may write state. This is the core principle that ensures predictability.
Advanced Usage
Async Actions
use *;
Scoped Stores
For multiple instances of the same store type:
// Provide scoped stores with unique IDs
;
;
// Access scoped stores
let counter1 = ;
let counter2 = ;
Store Registry
For debugging and hot-reloading:
let mut registry = new;
registry.register?;
// Later...
let store = registry.;
SSR Hydration
For full SSR applications, implement HydratableStore to enable automatic state transfer from server to client:
use *;
use *;
use ;
// State must derive Serialize and Deserialize
// Implement HydratableStore for SSR hydration
Or use the impl_hydratable_store! macro for less boilerplate:
use impl_hydratable_store;
impl_hydratable_store!;
Server-side (SSR):
// Provide store and render hydration script
let store = new_with_data;
let hydration_script = provide_hydrated_store;
view!
Client-side (Hydration):
// Automatically hydrate from server-rendered state
let store = ;
Design Philosophy
Convention over Primitives
Instead of giving you raw signals and hoping for the best, leptos-store provides a structured architecture that scales.
Compile-time Enforcement
The type system prevents invalid state transitions. If it compiles, it follows the rules.
SSR-First Design
Every feature is designed with server-side rendering in mind. No hydration mismatches.
Examples
See the examples/ directory for complete examples:
counter-example- Simple counter using thestore!macro with increment/decrementauth-store-example- User authentication flow with login/logouttoken-explorer-example- Full SSR with hydration - Real-time Solana token explorer using Jupiter API
Running Examples
# List all available examples
# Run a specific example (SSR mode with cargo-leptos)
# Build an example
Token Explorer Example
The token-explorer-example demonstrates full SSR hydration:
- 🌐 Server-side data fetching from Jupiter API
- 💧 Automatic state hydration to client
- 🔄 Client-side polling for real-time updates
- 🔍 Reactive filtering and sorting
- 🎨 Beautiful token card UI
# Run the token explorer
# Opens at http://127.0.0.1:3005
Contributing
We welcome contributions! See AUTHORING.md for:
- Development setup and workflow
- Project structure and architecture
- Testing and code quality guidelines
- Publishing releases
# Quick start for contributors
License and Attribution
leptos-store is licensed under the Apache License, Version 2.0.
You are free to use, modify, and distribute this software, including for commercial purposes, provided that you retain the license text and the NOTICE file as required by the Apache 2.0 License.
This software is provided "AS IS", without warranty of any kind. The author is not liable for any damages arising from its use.