Dioxus Provider
⚠️ In Development
This library is currently in active development. The API may change before the first stable release. Please check the changelog for the latest updates and breaking changes.
Effortless, powerful, and scalable data fetching and caching for Dioxus applications, inspired by Riverpod for Flutter.
dioxus-provider provides a simple yet robust way to manage data fetching, handle asynchronous operations, and cache data with minimal boilerplate. It is designed to feel native to Dioxus, integrating seamlessly with its component model and hooks system.
Key Features
Data Fetching & Caching
- Global Provider System: Manage application-wide data without nesting context providers. Simplifies component architecture and avoids "provider hell."
- Declarative
#[provider]Macro: Define data sources with a simple attribute. The macro handles all the complex boilerplate for you. - Intelligent Caching Strategies:
- Stale-While-Revalidate (SWR): Serve stale data instantly while fetching fresh data in the background for a lightning-fast user experience.
- Time-to-Live (TTL) Cache Expiration: Automatically evict cached data after a configured duration.
- Automatic Refresh: Keep data fresh with interval-based background refetching.
- Parameterized Queries: Create providers that depend on dynamic arguments (e.g., fetching user data by ID).
Composable Providers ✨ NEW!
- Parallel Execution: Run multiple providers simultaneously with
compose = [provider1, provider2, ...]for significant performance gains. - Type-Safe Composition: Automatic result combination with compile-time safety guarantees.
- Flexible Composition: Compose any subset of providers based on your specific needs.
- Error Aggregation: Intelligent error handling across composed providers with proper error propagation.
Structured Error Handling ✨ NEW!
- Rich Error Types: Comprehensive error hierarchy with
ProviderError,UserError,ApiError, andDatabaseError. - Actionable Error Messages: Context-rich error information for better debugging and user feedback.
- Error Chaining: Automatic error conversion and chaining using
#[from]attributes. - Backward Compatibility: Seamless integration with existing String-based error handling.
Mutation System
- Manual Implementation Pattern: Define data mutations using simple struct implementations.
- Optimistic Updates: Immediate UI feedback with automatic rollback on failure.
- Smart Cache Invalidation: Automatically refresh related providers after successful mutations.
- Mutation State Tracking: Built-in loading, success, and error states for mutations.
- Type-Safe Parameters: Support for no parameters, single parameters, and multiple parameters (tuples).
Developer Experience
- Manual Cache Control: Hooks to manually invalidate cached data or clear the entire cache.
- Cross-Platform by Default: Works seamlessly on both Desktop and Web (WASM).
- Minimal Boilerplate: Get started in minutes with intuitive hooks and macros.
- Type Safety: Full TypeScript-level type safety with Rust's type system.
Installation
Add dioxus-provider to your Cargo.toml:
[]
= "0.0.1" # Replace with the latest version
Getting Started
1. Initialize Global Providers
At the entry point of your application, call init_global_providers() once. This sets up the global cache that all providers will use.
use init_global_providers;
use *;
2. Create a Provider
A "provider" is a function that fetches or computes a piece of data. Use the #[provider] attribute to turn any async function into a data source that can be used throughout your app.
use *;
use Duration;
// This could be an API call, database query, etc.
async
3. Use the Provider in a Component
Use the use_provider hook to read data from a provider. Dioxus will automatically re-render your component when the data changes (e.g., when the async function completes).
The hook returns a Signal<AsyncState<T, E>>, which can be in one of three states: Loading, Success(T), or Error(E).
use *;
use *;
Mutations: Modifying Data with Automatic Cache Management
The mutation system allows you to define data modification operations that automatically invalidate related provider caches, ensuring your UI stays in sync with server state.
1. Basic Mutations (Macro-Based)
Create mutations using the #[mutation] attribute. Mutations automatically invalidate specified provider caches when they succeed.
use *;
// Define a mutation that invalidates the todo list when successful
async
// Use the mutation in a component
let = use_mutation;
2. Optimistic Updates
For better UX, use optimistic mutations that update the UI immediately and rollback on failure:
async
let = use_optimistic_mutation;
3. Multiple Cache Invalidation
Mutations can invalidate multiple provider caches at once:
async
New Features in Latest Release
Composable Providers: Parallel Data Loading
Run multiple providers simultaneously for better performance:
// These providers will run in parallel
async
Structured Error Handling
Rich, actionable error types for better error handling:
use *;
async
// Error types available: ProviderError, UserError, ApiError, DatabaseError
// Full backward compatibility with String errors
Advanced Usage
Parameterized Providers
Providers can take arguments to fetch dynamic data. For example, fetching a user by their ID. The cache is keyed by the arguments, so fetch_user(1) and fetch_user(2) are cached separately.
use *;
async
Caching Strategies
Stale-While-Revalidate (SWR)
stale_time serves cached (stale) data first, then re-fetches in the background. This provides a great UX by showing data immediately.
async
Cache Expiration (TTL)
cache_expiration evicts data from the cache after a time-to-live (TTL). The next request will show a loading state while it re-fetches.
// This data will be removed from cache after 5 minutes of inactivity
async
Manual Cache Invalidation
You can manually invalidate a provider's cache to force a re-fetch.
use *;
use *;
To clear the entire global cache for all providers:
let clear_cache = use_clear_provider_cache;
clear_cache;
Running The Examples
The examples directory contains comprehensive demos covering all library features.
Core Features
comprehensive_demo.rs: Showcases all provider features working together. A great place to start!counter_mutation_demo.rs: Complete mutation system demo with optimistic updates and cache invalidation.
Error Handling & Composition
structured_errors_demo.rs: NEW! Demonstrates comprehensive structured error handling withProviderError,UserError,ApiError, andDatabaseErrortypes. Shows proper error UI feedback and validation patterns.composable_provider_demo.rs: NEW! Showcases parallel provider composition for performance optimization. Demonstrates how to run multiple providers simultaneously and combine their results.dependency_injection_demo.rs: Shows manual dependency injection patterns withinject::<Type>()for clean provider architecture.
Caching & Performance
swr_demo.rs: Focuses on the Stale-While-Revalidate pattern for instant data loading.cache_expiration_demo.rs: Demonstrates TTL-based cache expiration strategies.interval_refresh_demo.rs: Shows automatic background data refreshing with configurable intervals.
To run an example:
# Run the comprehensive demo (recommended starting point)
# Try the new error handling demo
# See parallel provider composition in action
# Or run any specific demo
Ecosystem & Alternatives
dioxus-query: For Complex, Type-Safe Data Management
For more complex applications requiring advanced type safety, sophisticated caching strategies, and enterprise-grade data management, we highly recommend dioxus-query by Marc.
dioxus-query is a mature, production-ready library that provides:
- Advanced Type Safety: Compile-time guarantees for complex data relationships
- Sophisticated Caching: Multi-level caching with intelligent invalidation strategies
- Query Dependencies: Automatic dependency tracking and cascading updates
- Optimistic Updates: Immediate UI updates with rollback on failure
- Background Synchronization: Advanced background sync with conflict resolution
- Enterprise Features: Built-in support for complex data patterns and edge cases
When to choose dioxus-query:
- Large-scale applications with complex data requirements
- Teams requiring maximum type safety and compile-time guarantees
- Applications with sophisticated caching and synchronization needs
- Enterprise applications where data consistency is critical
When to choose dioxus-provider:
- Smaller to medium applications
- Quick prototyping and development
- Teams new to Dioxus data management
- Applications where simplicity and ease of use are priorities
dioxus-motion: For Smooth Animations and Transitions
Looking to add beautiful animations to your Dioxus application? Check out dioxus-motion - a lightweight, cross-platform animation library also built by me.
dioxus-motion provides:
- Cross-Platform Animations: Works seamlessly on web, desktop, and mobile
- Declarative Animation API: Write animations as data, not imperative code
- Page Transitions: Smooth route transitions with
AnimatedOutlet - Spring Physics: Natural, physics-based animations
- Custom Easing: Extensive easing function support
- Type-Safe Animations: Compile-time animation safety
- Extensible: Implement
Animatabletrait for custom types
Perfect combination:
- Use dioxus-provider for data fetching and caching
- Use dioxus-motion for smooth UI animations and transitions
- Both libraries work together seamlessly in the same application
// Example: Combining dioxus-provider with dioxus-motion
use *;
use *;
Acknowledgment
Special thanks to Marc for creating the excellent dioxus-query library, which has been a significant inspiration for this project. Marc's work on dioxus-query has helped establish best practices for data management in the Dioxus ecosystem, and we encourage users to explore both libraries to find the best fit for their specific use case.
Contributing
Contributions are welcome! Please feel free to open an issue or submit a pull request.
License
This project is licensed under the MIT License.