API Collection
createMapStore()

createMapStore

The createMapStore function creates a new global Map store instance.

function createMapStore<T>(props: ConstructorProps<T>): MapStore<T>;

Parameters

OptionTypeRequiredDescription
propsConstructorProps<T>YesConfiguration options for the store.

ConstructorProps<T>

OptionTypeRequiredDescription
initialDataMap<string, T>NoThe initial data for the store.
fallbackDataNestedPartial<T>NoFallback data used when a value is not found in the main data.
devToolsstringNoName 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.