Question : is it possible to listen to only one property change?
So far, it is possible to listen to "any" change in the localStorage, like this :
document.addEventListener(
"localDataStorage"
, nowICanSeeLocalStorageChangeEvents
, false
);
But is it also possible to listen to a change of one property only ?
document.addEventListener(
"myKey"
, myKeyListener
, false
);
Hey @bdavidxyz, thanks for the question!
Individual keys do not have specific change events associated with them.
The idea behind exposing every property (key names and key values) is so that you can determine what's important to monitor.
In your example, you specify the key you'd like to watch, but this does not require exposing a custom event handler just for that key. You can easily accomplish this level of monitoring with logic inserted into the suggested nowICanSeeLocalStorageChangeEvents function.
Just grab e.detail.oldkey to see the key that changed, and nab e.detail.newval to see how it changed. You can monitor any key in the store this way.