Comments (4)
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.
hmm I thought it was supposed to be something like window.addEventListener('storage', () => {
?
from localstorage.
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.
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)
- How to send facebook token(passport-facebook) from Frontend after I logged in?
- how inject in Code behind in program.cs HOT 5
- AddBlazoredLocalStorage() add public setter for options.JsonSerializerOptions HOT 2
- [Bug] LocalStorage crashes without throwing exception HOT 14
- [Bug] Impossible to make it work HOT 1
- [Bug] ASP.NET Core AggregateException on startup after injecting BlazorLocalStorage with dependency injection HOT 2
- [Bug] Performance Impact on Multiple SetItem Calls
- [Security] Consider updating Microsoft.AspNetCore.Components.Web dependency
- Net 8.0 iOS System.Text.Json.Serialization.JsonConverter System.ArgumentOutOfRangeException [Bug] HOT 1
- Unhandled exception rendering component: Please use dedicated worker for working with JavaScript interop. See https://aka.ms/dotnet-JS-interop-threads
- .Net 8 RenderMode.InteractiveServer CustomAuthenticationStateProvider[Bug] HOT 2
- Error in .NET 8 : Method not found HOT 3
- [Feature Request] Common Interface between ILocalStorageService and ISessionStorageService
- is it not working on net maui Hybrid? HOT 1
- [Bug] Lists of tuples are not being serialized correctly
- blazored.localstorage is missing NuGet package README file
- [Bug] Can't use local storage in a SSR HOT 1
- [Question] How can I use it in program.cs ? HOT 3
- Whether cookie operations can be added
- [Feature Request] An option to automatically add `location.pathname` to keys
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from localstorage.