Retrieve user data stored under a specific key
Returns the value previously saved with UserDataRepository.set.
The expected type of the stored data
The key identifying the data to retrieve
The stored data, or undefined if no data exists for the key
import { PlatformServiceName } from '@limetech/lime-web-components';
interface UserSettings {
notifications: boolean;
autoSave: boolean;
}
const userData = platform.get(PlatformServiceName.UserDataRepository);
const settings = userData.get<UserSettings>('settings');
// Nothing has been saved yet the first time the user opens the component, so
// read the values defensively
const notificationsEnabled = settings?.notifications === true;
const fontSize = userData.get<number>('fontSize') || 14;
Check if user data exists for a given key
Use this method to determine whether data has been previously saved under a specific key before attempting to retrieve it.
The key to check for stored data
true if data exists under the key, false otherwise
import { PlatformServiceName } from '@limetech/lime-web-components';
const userData = platform.get(PlatformServiceName.UserDataRepository);
// Offer the reset only when there is a saved layout to reset to
const canResetLayout = userData.has('dashboardLayout');
Store user data under a specific key
Saves the provided data to the server, where it will persist between sessions. The data is automatically serialized to JSON.
To delete previously saved data, pass undefined as the data parameter.
The type of data being stored
The key under which to store the data
Optionaldata: TThe data to save. Pass undefined to delete existing data
A promise that resolves when the data has been saved to the server
import { PlatformServiceName } from '@limetech/lime-web-components';
interface FilterSettings {
status: string[];
dateRange: { from: string; to: string };
}
const userData = platform.get(PlatformServiceName.UserDataRepository);
await userData.set('fontSize', 16);
await userData.set('showWelcome', false);
const filters: FilterSettings = {
status: ['active', 'pending'],
dateRange: { from: '2024-01-01', to: '2024-12-31' },
};
await userData.set('filters', filters);
import { PlatformServiceName } from '@limetech/lime-web-components';
const userData = platform.get(PlatformServiceName.UserDataRepository);
// Passing undefined as the value deletes the saved data
await userData.set('mySettings', undefined);
console.log(userData.has('mySettings')); // false
Subscribe to state changes with optional transformation and filtering.
The subscription will immediately invoke the callback with the current state (if any), then continue to call it whenever the state changes. The map and filter options allow you to transform and selectively receive updates.
Function called with state updates (after map/filter applied)
Optionaloptions: StateOptionsOptional transformations and filters for the subscription
Unsubscribe function - call this to stop receiving updates
import { PlatformServiceName } from '@limetech/lime-web-components';
const repository = platform.get(PlatformServiceName.Application);
const logger = platform
.get(PlatformServiceName.Logger)
.createLogger('my-component');
// Basic subscription
const unsubscribeState = repository.subscribe((state) => {
logger.debug('State updated', { state });
});
// With transformations
const unsubscribeUserName = repository.subscribe(
(userName) => logger.debug('User', { userName }),
{ map: [(state) => state.currentUser?.fullname] }
);
Service for storing and retrieving user-specific data that persists between sessions
The UserDataRepository provides a key-value store for saving user preferences, UI state, component settings, and other data that should persist across browser sessions. This is useful for remembering user choices, UI customizations, and component-specific settings.
Data is stored per user and persists on the server, so users will see their saved data regardless of which device or browser they use.
Since UserDataRepository extends StateRepository, components can use the SelectUserData decorator to automatically re-render when user data changes.
Example: Storing and retrieving user preferences
Example: Re-rendering when user data changes
See
StateRepository for subscription patterns