createRainbowStore is the foundational store creator for client-side state management in Rainbow. It provides a simple, type-safe way to create stores with optional persistence.
Overview
createRainbowStore is built on Zustand with these enhancements:
- Optional MMKV persistence - Automatically save and restore state
- Throttled persistence - Debounced writes to avoid excessive I/O
- Selective persistence - Choose which state to persist
- Type safety - Full TypeScript support
- Equality checking - Built-in
Object.isfor shallow equality
Basic Usage
In Components
Outside Components
Type Signatures
State Creator Function
set- Function to update stateget- Function to read current stateapi- Store API (subscribe, setState, etc.)
Persistence
Add persistence by providing a configuration object:Persistence Configuration
Selective Persistence
By default, all state except functions is persisted. Usepartialize to customize:
Persistence Throttling
Persistence is throttled to avoid excessive disk writes:src/state/internal/createRainbowStore.ts
Migrations
Handle schema changes with migrations:Custom Serialization
By default, Rainbow stores handleMap and Set automatically. Provide custom serializers for complex types:
Real-World Examples
Navigation Store
src/state/navigation/navigationStore.ts
Browser Store with Persistence
Advanced Patterns
Computed State
Async Actions
Middleware Pattern
Storage Implementation
Under the hood,createRainbowStore uses rainbowStorage (MMKV):
src/state/internal/createRainbowStore.ts
Best Practices
Use TypeScript interfaces
Use TypeScript interfaces
Always define explicit types for your store state:
Keep state flat
Keep state flat
Avoid deeply nested state for better performance:
Use selectors wisely
Use selectors wisely
Select only what you need to avoid unnecessary re-renders:
Export methods for external use
Export methods for external use
Export commonly-used actions:
API Reference
Store Methods
Persisted Store Methods
Next Steps
createQueryStore
Learn about data fetching stores
Storage System
Deep dive into MMKV storage