State Management Module
The state management module provides a reactive state management system for React Express applications. It enables real-time state synchronization between the client and server, with automatic DOM updates and subscription capabilities.
Features
- Reactive state management
- Real-time state synchronization
- Automatic DOM updates
- State subscriptions
- Batch updates
- Type-aware rendering
API Reference
State Management
ReactExpress.setState(key, value, options = { sync: true })
Sets a state value and updates all subscribed elements.
javascript
ReactExpress.setState('counter', 5);
ReactExpress.setState('user', { name: 'John' }, { sync: false });
Parameters:
key
(string): State identifiervalue
(any): New state valueoptions
(object):sync
(boolean): Whether to sync with server (default: true)
ReactExpress.getState(key)
Retrieves the current value of a state.
javascript
const value = ReactExpress.getState('counter');
ReactExpress.subscribe(keys, callback)
Subscribes to changes in multiple state values.
javascript
ReactExpress.subscribe(['counter', 'name'], ([count, name]) => {
return `${name}: ${count}`;
});
Parameters:
keys
(string[]): Array of state keys to subscribe tocallback
(function): Function called when any subscribed state changes
ReactExpress.batchUpdate(updates, options = { sync: true })
Updates multiple state values at once.
javascript
ReactExpress.batchUpdate({
counter: 5,
name: 'John',
active: true
});
Parameters:
updates
(object): Key-value pairs of state updatesoptions
(object):sync
(boolean): Whether to sync with server (default: true)
HTML Integration
Data Attributes
data-react-state
Binds an element to a state value:
html
<div data-react-state="counter">0</div>
<div data-react-state="user" data-type="json"></div>
data-type
Specifies how to render complex state values:
json
: Pretty-prints JSON objectslist
: Renders arrays as list itemstodo
: Special rendering for todo items
Type-Specific Rendering
The state module includes specialized rendering for different data types:
JSON Objects
html
<div data-react-state="user" data-type="json"></div>
Lists
html
<ul data-react-state="items" data-type="list"></ul>
Todo Items
html
<div data-react-state="todos" data-type="todo"></div>
Server Synchronization
The state module automatically synchronizes with the server when enabled:
- Real-time updates via WebSocket
- Initial state loading on page load
- Batch updates for performance
- Error handling and recovery
Best Practices
- Use batch updates when changing multiple states
- Consider disabling sync for temporary local states
- Provide appropriate data-type attributes for complex data
- Use subscriptions for computed values
- Handle initial state loading properly