Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question : is it possible to listen to only one property change? #2

Open
bdavidxyz opened this issue Jun 29, 2023 · 1 comment
Open

Comments

@bdavidxyz
Copy link

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
);
@macmcmeans
Copy link
Owner

macmcmeans commented Aug 11, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants