From bce7832a118f9b0d267df7b0807edfe0af799bd6 Mon Sep 17 00:00:00 2001 From: Gunes Date: Mon, 10 Jul 2023 12:47:13 +0300 Subject: [PATCH] Simple Update for state management In my own tests, using OnChange += StateHasChanged; unfortunately did not work. Based on this, I did some research and came to the conclusion that when we write our own handler method, we can solve this problem with invoke async. If we can update the document in this way, it will be better for people like me who come across this problem to learn directly with the already solved version. --- aspnetcore/blazor/includes/state-container.md | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/aspnetcore/blazor/includes/state-container.md b/aspnetcore/blazor/includes/state-container.md index e22705b95cd4..c6b374d52e03 100644 --- a/aspnetcore/blazor/includes/state-container.md +++ b/aspnetcore/blazor/includes/state-container.md @@ -66,7 +66,7 @@ services.AddScoped(); @code { protected override void OnInitialized() { - StateContainer.OnChange += StateHasChanged; + StateContainer.OnChange += OnMyChangeHandler; } private void ChangePropertyValue() @@ -77,7 +77,14 @@ services.AddScoped(); public void Dispose() { - StateContainer.OnChange -= StateHasChanged; + StateContainer.OnChange -= OnMyChangeHandler; + } + + private async void OnMyChangeHandler() + { + await InvokeAsync(() => { + StateHasChanged(); + }); } } ``` @@ -104,7 +111,7 @@ services.AddScoped(); @code { protected override void OnInitialized() { - StateContainer.OnChange += StateHasChanged; + StateContainer.OnChange += OnMyChangeHandler; } private void ChangePropertyValue() @@ -115,7 +122,14 @@ services.AddScoped(); public void Dispose() { - StateContainer.OnChange -= StateHasChanged; + StateContainer.OnChange -= OnMyChangeHandler; + } + + private async void OnMyChangeHandler() + { + await InvokeAsync(() => { + StateHasChanged(); + }); } } ```