-
-
Notifications
You must be signed in to change notification settings - Fork 135
Description
Hello @jmeistrich - it's me again!
I have a particular situation where for e.g. if I have set the initial state to an empty object for e.g. {} and if I do state$.reset() it will will delete everything from my localstorage but the app itself will not re-render and clear up. If I do a full page reload, it will then clear of course.
The behaviour does not happen if I have a non-empty initial object. It then resets to the default value set in the initial object no problem. Is this the expected behaviour? 🤔
This is my example code below. If initialCategories is an empty object initially and I populate it with different categories and try to reset state, it will clear localstorage but will not re-render my component. If initialCategories has a default value like in the example below, it will reset to that state and re-render the component to display the initial hard-coded state.
'use client'
import { observable, syncState } from '@legendapp/state'
import { syncObservable, synced } from '@legendapp/state/sync'
import { StorageEngine } from './storageEngine/storageEngine'
import { profile$ } from './profile'
import { uuidv7 } from 'uuidv7'
import { z } from 'zod'
export const CategorySchema = z.object({
uid: z.string(),
userId: z.string(),
name: z.string().min(1, 'Name is required').max(20, 'Name must be less than 20 characters'),
icon: z.string(),
createdAt: z.number(),
updatedAt: z.number(),
deleted: z.boolean().default(false),
})
export type Category = z.infer<typeof CategorySchema>
interface CategoryRecord {
[key: string]: Category
}
const initialCategories: CategoryRecord = {
'01940670-e312-7fd1-bb87-27c5e6ebb4fb': {
uid: '01940670-e312-7fd1-bb87-27c5e6ebb4fb',
userId: profile$.uid.get(),
name: 'Art',
icon: 'Inspiration',
createdAt: Date.now(),
updatedAt: Date.now(),
deleted: false,
}
}
export const categories$ = observable<CategoryRecord>(
synced({
initial: initialCategories,
persist: {
name: 'categories',
plugin: StorageEngine,
retrySync: true,
},
})
)
export const categoriesState$ = syncState(categories$)