Changelog
0.3.0
Minor Changes
-
ddad467: First public release on npm, continuing from 0.2.1, which was published privately on GitHub Packages. Every 0.x minor may contain breaking changes, so pin the version you depend on. To upgrade from 0.2.x, follow Migrating; values already stored need no migration.
Fixed:
- Strings such as
"42"or"true"are stored as JSON and read back as strings, not as a number or a boolean. Raw strings written by 0.2.1 still read as strings whendefaultis a string. localStorageandsessionStoragestate no longer causes a hydration mismatch. The server snapshot is always the default, and the stored value follows right after hydration.- No provider and no
next-client-cookiesare needed to use the package.
Added:
persistentState()declares a key once with its type, storage and options.useStoredState()from/reactuses it in components,readState()andPersistentStateProviderfrom/nextread cookie state on the server, andreadCookieState()andCookieValuesProviderdo the same in other runtimes and frameworks.reset(), an in-memory fallback when storage is unavailable, and anisPersistentflag.- Cross-tab sync, on by default and including cookies; pass
sync: falseto turn it off. - Validation with any Standard Schema through
schema,versionandmigrate, andexpiresforlocalStorageandsessionStorage. - An optional
consentpredicate andrefreshConsent(). Whileconsentreturnsfalse, storage is neither read nor written, and nothing is deleted automatically.
Breaking:
- Install from npm. Remove the
@nicolopadovan:registry=https://npm.pkg.github.comline and the read token from.npmrc. usePersistentStatemoved from the root entry to@nicolopadovan/persistent-state/reactand returns a third element,{ reset, isPersistent }.- Removed
PersistentStateCookiesProviderand thenext-client-cookiespeer dependency. To render cookie state on the server in Next.js, usePersistentStateProviderfrom/nextwithstates. - Removed
useStorage,StorageAPI,StorageOptions,CookieStorageOptionsandNonCookieStorageOptions. UsepersistentState()and itsget,setandreset. syncdefaults totrue, wheresyncAcrossTabsdefaulted tofalse. Passsync: falsefor the old behavior.- A cookie’s
expiresdefaults to 365 days, where a cookie withoutexpireswas a session cookie.expires: 0writes a session cookie. ForlocalStorageandsessionStorage,expires: 0means no expiry. - A
nullstored by 0.2.1 now reads asnull, where 0.2.1 used the default, unlessdefaultis a string. - The legacy rule for raw strings follows the type of
default. A state whose default is not a string, such asnulltypedstring | null, reads a raw 0.2.1 value that parses as JSON (42,true) as that number or boolean, and aschemareports it asinvalid. Save the value again, or give the state a stringdefault.
Deprecated, removed in 1.0.0:
getPersistentCookieItem,getPersistentCookieValue,PersistentCookieStore,StorageType, and thestorageTypeandsyncAcrossTabsoptions ofusePersistentState. - Strings such as