Replies: 2 comments 5 replies
-
Currently, if the Zustand state is set, For the same reason we introduced the |
Beta Was this translation helpful? Give feedback.
-
Thanks @AnatoleLucet. I guess my main question is does it make sense for The side effect of this is if a user is doing an API call in the middleware and using a remote DB as their persistence store. If Zustand is calling setState every time any part of the store changes, then there will be API calls to store data even if it's the exact same data that is already in the DB. For one user this isn't a big deal, but at scale, when multiple users are using the app and hitting the DB, that could potentially add up to quite a cost for the applications backend. |
Beta Was this translation helpful? Give feedback.
-
I have the
persist
middleware set up with a custompartialize
function that will only return properties that I actually want to persist. The issue I'm seeing is thatsetItem
is still called even if the properties being persisted haven't changed.For example, lets take this dummy state object:
Within the
partialize
function, we add:Effectively saying that we don't want to persist the
currentUser
property.However, whenever the
currentUser
property changes, thesetItem
function ingetStorage
ofpersist
will still be called, even though the data being stored hasn't actually changed.I assume it's likely due to
Object.fromEntries
creating a new object, so a shallow comparison will always indicate false.Is there a way to memoize this data at the
partialize
step, sosetItem
isn't called unless the data being saved has actually changed?Beta Was this translation helpful? Give feedback.
All reactions