GithubHelp home page GithubHelp logo

Comments (4)

chrissainty avatar chrissainty commented on May 16, 2024

Hi @Eonasdan, this isn’t a bug the event isn’t designed to fire across tabs. The Changing event is a C# event, when you open a different tab you’re running another instance of the app. There is no way for it to know about an event in the other instance.

from localstorage.

Eonasdan avatar Eonasdan commented on May 16, 2024

hmm I thought it was supposed to be something like window.addEventListener('storage', () => { ?

from localstorage.

chrissainty avatar chrissainty commented on May 16, 2024

There is a JavaScript event, I assume you’re thinking of this one. This is fired automatically by the browser but we don’t hook into it in any way. As I said, all our events are in C#.

If this is what you’re trying to hook onto then you would need to do this via JavaScript interop.

from localstorage.

pekspro avatar pekspro commented on May 16, 2024

Here is an example how to set this up. Note that the event will be triggered in other tabs, not the tab were the storage has been changed.

In JavaScript:

window.addEventListener('storage', function (e) {
    if (e.storageArea === localStorage) {
        if (e.key !== 'i18next.translate.boo') {
            DotNet.invokeMethodAsync('MyAssembly', 'LocalStorageChanged', e.key, e.key, e.oldValue, e.newValue);
        }
    }
});

i18next.translate.boo has something to do with translations and is used by Blazor. MyAssembly should be the name of the assembly.

In C#:

using Microsoft.JSInterop;

public static class JavaScriptCallbacks
{
    [JSInvokable]
    public static Task LocalStorageChanged(string? key, string? oldValue, string? newValue)
    {
        Console.WriteLine($"Method MyMethod has been invoked with key: {key}");
        return Task.CompletedTask;
    }
}

However, this C# solution might be to simple. You probably want to interactive with the change in your Blazor components. If so:

public static class JavaScriptCallbacks
{
    public delegate void StorageChangedHandler(string? key, string? oldValue, string? newValue);

    public static event StorageChangedHandler? OnStorageChanged;

    [JSInvokable]
    public static void LocalStorageChanged(string? key, string? oldValue, string? newValue)
    {
        OnStorageChanged?.Invoke(key, oldValue, newValue);

        Console.WriteLine($"Method MyMethod has been invoked with key: {key}");
    }
}

And then in you Blazor component:

   @implements IDisposable

    protected override void OnInitialized()
    {
        JavaScriptCallbacks.OnStorageChanged += OnStorageChangedCallback;
    }

    private void OnStorageChangedCallback(string? key, string? oldValue, string? newValue)
    {
        InvokeAsync(() => OnStorageChangedAsync(key, oldValue, newValue));
    }

    private async Task OnStorageChangedAsync(string? key, string? oldValue, string? newValue)
    {
        Console.WriteLine("Storage changed: " + key);
        StateHasChanged();
    }

    public void Dispose()
    {
        // Don't forget to unsubscribe when the component is disposed to avoid memory leaks.
        JavaScriptCallbacks.OnStorageChanged -= OnStorageChangedCallback;
    }

from localstorage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.