Overview
ThestateManager is an optional configuration that lets you persist user interactions across sessions or page refreshes. When a user applies filters, changes pages, or adjusts settings, you can save that state so the list looks exactly how they left it.
By default, VueList keeps state in memory only - it resets on page refresh or route changes. With a stateManager, you can persist state to:
- localStorage - Persists across sessions
- sessionStorage - Persists until browser tab closes
- Vuex/Pinia - Sync with your state management
- API - Save to backend for cross-device persistence
The
stateManager is completely optional. VueList works perfectly fine without it - you only add it when you need state persistence.Configuration
ThestateManager is an object with three methods:
Called once when the component mounts. Use it to clean up old/stale states.
Called when the component mounts to restore previous state. Should return the saved state object or
null.Called whenever state changes (page, filters, search, etc.). Should save the context to storage.
The Context Parameter
All three methods receive the samecontext object. The context includes:
Unique identifier for the list (typically the API endpoint)
Version number from the
<VueList version="..." /> propCurrent page number
Items per page
Current search query
Field to sort by
Sort direction
Applied filters
Column visibility and settings
Additional metadata from the component
Examples
Basic localStorage Implementation
main.js
Production-Ready Implementation with Versioning
This example from the VueList playground shows a complete implementation with version-based cleanup:vue-list.js
The version-based approach is crucial for production apps. When you update your list configuration, increment the version number to invalidate old saved states.
Using sessionStorage
For state that should only persist during the browser session:main.js
Integration with Pinia
main.js
API-Based Persistence
For cross-device state synchronization:main.js
How VueList Uses State Manager
On Component Mount
- VueList calls
init(context)to allow cleanup of stale states - VueList calls
get(context)to restore previous state - If state is found, it overrides initial prop values (except
pageif in URL) - VueList fetches data with the restored/initial state
On State Change
Whenever the user interacts with the list (changes page, applies filter, searches, etc.):- VueList updates internal state
- VueList calls
set(context)with the new context - Your
stateManagersaves the new state
What Gets Persisted
The following properties are typically saved and restored:page- Current page numberperPage- Items per pagesearch- Search querysortBy- Sort fieldsortOrder- Sort directionfilters- Applied filtersattrSettings- Column visibility settings
endpoint and version are used as keys to identify which state to load, but are not typically saved in the state itself.
Version Management
Theversion prop on <VueList> is crucial for state management:
ProductList.vue
Why use versions?
Why use versions?
When you change your list configuration (add new filters, change default sort, etc.), old saved states may be incompatible. Incrementing the version number tells VueList to ignore old states and start fresh.
When to increment the version?
When to increment the version?
- Adding or removing filters
- Changing default values
- Modifying the data structure
- Any breaking change to list configuration
What happens to old states?
What happens to old states?
The
init() method can clean up old version states to prevent localStorage bloat. See the production example above for implementation.Default Implementation
If you don’t provide astateManager, VueList uses these no-op functions:
options.js
Best Practices
Always use try-catch in get()
Always use try-catch in get()
localStorage can fail or contain corrupted data. Always wrap
JSON.parse() in a try-catch and return null on error.Include version in storage keys
Include version in storage keys
This allows you to invalidate old states when your list configuration changes.
Clean up stale states in init()
Clean up stale states in init()
Prevent localStorage bloat by removing old version states during initialization.
Don't persist sensitive data
Don't persist sensitive data
Avoid saving sensitive information in localStorage. Consider using sessionStorage or API-based persistence for sensitive lists.
Test with and without saved state
Test with and without saved state
Always test both the “first visit” and “returning user” scenarios to ensure your state restoration works correctly.
Consider debouncing API calls
Consider debouncing API calls
If using API-based persistence, debounce the
set() calls to avoid excessive server requests.Debugging State Issues
If state persistence isn’t working:- Check browser console for localStorage errors
- Verify the storage key format matches between
get()andset() - Ensure
versionprop is set on the component - Check that
get()returns an object (not a string) - Verify
init()isn’t accidentally clearing the current version
Related
- Configuration Overview - Complete plugin setup guide
- VueList Component - See the
versionprop documentation - Request Handler - How data fetching works