How to get updated property in singleton from a component in Blazor?

Marco Jr

I just changed the default Blazor sample to illustrate this issue.

MainLayout.razor (This is the top of the page, just replaced the About to display the counter)

@inherits LayoutComponentBase
@inject TestClass testClass
<div class="sidebar">
   <NavMenu />
</div>

<div class="main">
  <div class="top-row px-4">
    Counter: @testClass.Counter
</div>

<div class="content px-4">
    @Body
</div>

Counter.razor (This one was modified to instead use the local variable, invoke the method to increment via class and display the value from the class)

@page "/counter"
@inject TestClass testClass
<h1>Counter</h1>

<p>Current count:@testClass.Counter</p>

<button class="btn btn-primary" @onclick="(e => testClass.Increment())">Click me</button>

@code {

}

TestClass.cs (This one is the class with the current value and method to increment)

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;

namespace TestBlazor.Classes
{
   public class TestClass
    {
     public int Counter { get; set; } = 10;
     public void Increment() {
        Counter++;
     }
 }
}

This is the result after few presses on the increment button:

enter image description here

So, the page itself can display the current value every time it's changes, however the component on the top of the page cannot. It's just displays the default value of 10.

This is also my Program.cs

using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using TestBlazor.Classes;

namespace TestBlazor
{
  public class Program
  {
    public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        builder.RootComponents.Add<App>("app");

        builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
        builder.Services.AddSingleton<TestClass>();

        await builder.Build().RunAsync();
    }
  }
}

Using Core5

enet

You'll need to notify the MainLayout component that its state has changed, and that he should re-render. This is doable by defining an event delegate that is executed whenever the counter variable is incremented:

TestClass.cs

public class TestClass
    {
        public int Counter { get; set; } = 10;
        public void Increment()
        {
            Counter++;
            Notify?.Invoke();
        }
        public event Action Notify;
    }

Add this code to the MainLayout page

@code{
    protected override void OnInitialized()
    {
        testClass.Notify += () => InvokeAsync(StateHasChanged);

        base.OnInitialized();
    }
}

You'll also have to implement IDisposable

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Angular Parent Component property does not get updated when changing value from Child Component

From Dev

How to get property from component when testing?

From Dev

emberjs component property not updated?

From Dev

How can I get a child component's watcher's updated value from parent component in Vue 2?

From Dev

How to bind a property to a singleton object property from QML

From Dev

ReactJS: How to get property in a component container?

From Dev

React - how to get real DOM element after component is updated

From Dev

How to access the "key" property from a reactjs component

From Dev

How to change controller property from component?

From Dev

Emberjs How to update property on a component from route?

From Dev

How to get component from component name in codenameone?

From Dev

How to get a property from another property in JS?

From Dev

How to navigate to a blazor component from a razor page in a Blazor server-side app?

From Dev

How to get property from ofono

From Dev

How to get a property from a plist

From Dev

How to get a property from a plist

From Dev

Get CSS property values from a component's "style" prop

From Dev

How can get instance from singleton class with reflection

From Dev

How to get string of x:Name property of component in Xamarin form?

From Java

How to force Entity Framework to always get updated data from the database?

From Dev

How whatsapp get updated contacts from addressbook faster in iOS?

From Dev

How to get updated list values from JSP in my action?

From Dev

How to get updated object from NHibernate session in Global Filter

From Dev

How to get updated variable by a python function from another python function?

From Dev

How whatsapp get updated contacts from addressbook faster in iOS?

From Dev

How to call php function to get updated data from MYSQL with jQuery

From Dev

how to get data from a variable in Def to use it outside and remains updated?

From Java

How to get data from function component?

From Dev

How to get component value from ActionEvent object?

Related Related

  1. 1

    Angular Parent Component property does not get updated when changing value from Child Component

  2. 2

    How to get property from component when testing?

  3. 3

    emberjs component property not updated?

  4. 4

    How can I get a child component's watcher's updated value from parent component in Vue 2?

  5. 5

    How to bind a property to a singleton object property from QML

  6. 6

    ReactJS: How to get property in a component container?

  7. 7

    React - how to get real DOM element after component is updated

  8. 8

    How to access the "key" property from a reactjs component

  9. 9

    How to change controller property from component?

  10. 10

    Emberjs How to update property on a component from route?

  11. 11

    How to get component from component name in codenameone?

  12. 12

    How to get a property from another property in JS?

  13. 13

    How to navigate to a blazor component from a razor page in a Blazor server-side app?

  14. 14

    How to get property from ofono

  15. 15

    How to get a property from a plist

  16. 16

    How to get a property from a plist

  17. 17

    Get CSS property values from a component's "style" prop

  18. 18

    How can get instance from singleton class with reflection

  19. 19

    How to get string of x:Name property of component in Xamarin form?

  20. 20

    How to force Entity Framework to always get updated data from the database?

  21. 21

    How whatsapp get updated contacts from addressbook faster in iOS?

  22. 22

    How to get updated list values from JSP in my action?

  23. 23

    How to get updated object from NHibernate session in Global Filter

  24. 24

    How to get updated variable by a python function from another python function?

  25. 25

    How whatsapp get updated contacts from addressbook faster in iOS?

  26. 26

    How to call php function to get updated data from MYSQL with jQuery

  27. 27

    how to get data from a variable in Def to use it outside and remains updated?

  28. 28

    How to get data from function component?

  29. 29

    How to get component value from ActionEvent object?

HotTag

Archive