Collection

Setup

Install package

npm install @devlab/store

Usage

import { createMapStore } from '@devlab/store'
 
type UserData = {
  name: string;
  job?: string;
}
 
const usersStore = createMapStore<UserData>({
  devtools: "Users Store",
  initialData: new Map([
    ['john', { name: 'John' }],
    ['jane', { name: 'Jane', job: 'Developer' }]
  ]),
  fallbackData: { name: "Guest", job: "Unemployed" }
})
 
export function App() {
  const totalUsers = usersStore.useSize()
  const usersIds = usersStore.useKeys()
 
  return (
    <div>
      <h1>Total Users: {totalUsers}</h1>
      <div>Users: {usersIds.join(', ')}</div>
 
      <UserProfile userId="john" />
    </div>
  )
}
 
export function UserProfile({ userId }) {
  const name = usersStore.key(userId).use('name')
 
  return (
    <div>
      <h1>{name}</h1>
    </div>
  )
}