I'm working on a cross-platform project using Blazor WebAssembly and .NET MAUI Blazor Hybrid (targeting .NET 8). I'm using Fluxor for state management across both environments.
In my shared component library, I have a base class called SettingsBase that looks something like this:
public class SettingsBase : ComponentBase{ protected void OnStateChanged(object? sender, EventArgs e) { StateHasChanged(); }}
All of my Razor components inherit from SettingsBase. In each component, I register for state change notifications like this:
ApplicationState.StateChanged += OnStateChanged;
The idea is that whenever any relevant state changes, the OnStateChanged method triggers StateHasChanged(), causing the component to re-render — and this works perfectly in Blazor WebAssembly.
However, when I run the same shared components in a .NET MAUI Blazor Hybrid app, I get the following exception:
System.NullReferenceException at Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged()
It seems like StateHasChanged() is being called before the component is fully initialized or attached to the Blazor rendering system in MAUI.
My questions:Has anyone else encountered this issue when using StateHasChanged() in shared components between Blazor WASM and MAUI?
Does MAUI Blazor Hybrid handle component lifecycle differently in a way that affects StateHasChanged()?
Is there a best practice for subscribing to state changes in shared Razor components that avoids this issue?