💾 Persistence
You can persist data across sessions by saving it in a storage medium like localStorage
, sessionStorage
, or other available storage options. This ensures that data is retained even when the page is reloaded or the browser is closed and reopened.
Additionally, it's possible to synchronize state across different tabs or windows. When a change is made in one tab or window, all others sharing the same storage context are updated accordingly. This allows seamless state updates across multiple views.
import { create, persist } from "@bentoo/state-man";
const usePreferenceStore = create(
persist({
name: "storeKey",
data: { theme: "dark" },
storage: sessionStorage
})
);
This approach ensures both persistence and synchronization, providing a consistent experience for users working across multiple tabs or windows.
persist
Property | Type | Description |
---|---|---|
name | string | Unique store name used as a key to store and retrieve data from the selected storage. |
data | any | The initial state value of the store. Can be any data type (number, string, object, array, etc.). |
storage | Storage | The storage type used for persistence. Defaults to localStorage . Can be localStorage , sessionStorage , etc. |