createMapStore
The createMapStore
function creates a new global Map store instance.
function createMapStore<T>(props: ConstructorProps<T>): MapStore<T>;
Parameters
Option | Type | Required | Description |
---|---|---|---|
props | ConstructorProps<T> | Yes | Configuration options for the store. |
ConstructorProps<T>
Option | Type | Required | Description |
---|---|---|---|
initialData | Map<string, T> | No | The initial data for the store. |
fallbackData | NestedPartial<T> | No | Fallback data used when a value is not found in the main data. |
devTools | string | No | Name for Redux DevTools integration. If provided, enables DevTools support. |
Returns
MapStore<T>
: A new Map store instance.
Description
The createMapStore
function creates a new global Map store instance that can be used throughout your application. It initializes the store with the provided initial data and configuration options.
Example
import { createMapStore } from '@devlab/store';
type UserData = {
name: string;
age: number;
preferences: {
theme: 'light' | 'dark';
};
};
const userStore = createMapStore<UserData>({
initialData: new Map([
['user1', { name: 'John Doe', age: 30, preferences: { theme: 'light' } }],
['user2', { name: 'Jane Doe', age: 28, preferences: { theme: 'dark' } }]
]),
fallbackData: {
name: 'Guest',
age: 0,
preferences: {
theme: 'light'
}
},
devTools: 'User Map Store' // Enables Redux DevTools integration
});
// Now you can use the store methods
userStore.key('user3').set({ name: 'Alice Smith', age: 35, preferences: { theme: 'light' } });
const userName = userStore.key('user1').get('name');
console.log(userName); // 'John Doe'
// In a React component
function UserProfile({ userId }: { userId: string }) {
const name = userStore.key(userId).use('name');
const age = userStore.key(userId).use('age');
return (
<div>
<p>Name: {name}</p>
<p>Age: {age}</p>
</div>
);
}
This example demonstrates how to create a global Map store, set and get values, and use it within a React component.