Accessing previous value in persist setItem #2876
Unanswered
TeemuKoivisto
asked this question in
Q&A
Replies: 1 comment 16 replies
-
I wonder if creating a custom storage would work it around: const createCustomStorage = () => {
let prevValue;
return {
getItem: (name) => {
...
},
setItem: (name, newValue) => {
...
prevValue = newValue;
},
removeItem: (name) => {
...
},
};
}; |
Beta Was this translation helpful? Give feedback.
16 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey, I've been a happy user for a zustand for some time now. Just now I've started to integrate my zustand stores with IndexedDB (through Dexie) and I noticed one rather annoying aspect of it.
Mainly, as I am using
import { persist } from 'zustand/middleware'
andsetItem: (name, newValue: StorageValue<State>) => {
to update my IndexedDB, there's no built-in way of accessing the previous value of the state.This would be rather useful as I can't reliably check what has changed so I have to run complicated
db.table.bulkPut
logic for every tiniest change that might not change anything in IDB. The performance implications of this might not be huge although with large in-memory datasets and lot of updates to the store, it could cause unnecessary CPU & memory use on mobile devices especially.Could this value be provided as 3rd argument?
Beta Was this translation helpful? Give feedback.
All reactions