mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-07 22:24:06 -05:00
* feat(cursorrules): updated cursorrules and claude md file * added rules for adding new integrations
71 lines
1.7 KiB
Plaintext
71 lines
1.7 KiB
Plaintext
---
|
|
description: Zustand store patterns
|
|
globs: ["apps/sim/**/store.ts", "apps/sim/**/stores/**/*.ts"]
|
|
---
|
|
|
|
# Zustand Store Patterns
|
|
|
|
Stores live in `stores/`. Complex stores split into `store.ts` + `types.ts`.
|
|
|
|
## Basic Store
|
|
|
|
```typescript
|
|
import { create } from 'zustand'
|
|
import { devtools } from 'zustand/middleware'
|
|
import type { FeatureState } from '@/stores/feature/types'
|
|
|
|
const initialState = { items: [] as Item[], activeId: null as string | null }
|
|
|
|
export const useFeatureStore = create<FeatureState>()(
|
|
devtools(
|
|
(set, get) => ({
|
|
...initialState,
|
|
setItems: (items) => set({ items }),
|
|
addItem: (item) => set((state) => ({ items: [...state.items, item] })),
|
|
reset: () => set(initialState),
|
|
}),
|
|
{ name: 'feature-store' }
|
|
)
|
|
)
|
|
```
|
|
|
|
## Persisted Store
|
|
|
|
```typescript
|
|
import { create } from 'zustand'
|
|
import { persist } from 'zustand/middleware'
|
|
|
|
export const useFeatureStore = create<FeatureState>()(
|
|
persist(
|
|
(set) => ({
|
|
width: 300,
|
|
setWidth: (width) => set({ width }),
|
|
_hasHydrated: false,
|
|
setHasHydrated: (v) => set({ _hasHydrated: v }),
|
|
}),
|
|
{
|
|
name: 'feature-state',
|
|
partialize: (state) => ({ width: state.width }),
|
|
onRehydrateStorage: () => (state) => state?.setHasHydrated(true),
|
|
}
|
|
)
|
|
)
|
|
```
|
|
|
|
## Rules
|
|
|
|
1. Use `devtools` middleware (named stores)
|
|
2. Use `persist` only when data should survive reload
|
|
3. `partialize` to persist only necessary state
|
|
4. `_hasHydrated` pattern for persisted stores needing hydration tracking
|
|
5. Immutable updates only
|
|
6. `set((state) => ...)` when depending on previous state
|
|
7. Provide `reset()` action
|
|
|
|
## Outside React
|
|
|
|
```typescript
|
|
const items = useFeatureStore.getState().items
|
|
useFeatureStore.setState({ items: newItems })
|
|
```
|