Calling StateHasChanged() on parent component from child component

huangapple go评论49阅读模式
英文:

Calling StateHasChanged() on parent component from child component

问题

在我的 Blazor 应用程序中,用于管理案例的页面上,我有一个名为 ClaimedBy 的属性,它位于一个名为 CaseDetails.cs 的类的实例中,该属性存储了已认领案例的管理员的用户名。

在首页上显示如下:
<p>认领人:@caseDetails.ClaimedBy</p>

现在,从一个子组件中,我尝试像这样更新 ClaimedBy 的值:
caseDetails.ClaimedBy = newUser;

但是首页上的名称(父组件)不会自动更新。如果我在首页上创建一个按钮,通过调用 StateHasChanged() 方法,它会正确更新,如下所示:

<button @onclick="UpdatePage">更新页面</button>

@code {
    private void UpdatePage()
    {
        StateHasChanged();
    }
}

我尝试从我的子组件中调用 StateHasChanged(),在设置 ClaimedBy 属性的新值的相同方法中,但这并不起作用。StateHasChanged() 必须从父组件中调用。如何让它在设置新值后自动更新?

英文:

In my blazor app for administrating cases, I have a property ClaimedBy on an instance of a class CaseDetails.cs which holds the username of the administrator who has claimed the case.

It is displayed like this on the index page:
<p>Claimed by: @caseDetails.ClaimedBy</p>

Now, from a child component, I'm trying to update the value of ClaimedBy like this:
caseDetails.ClaimedBy = newUser;

But the name on the index page (the parent component), is not updated automatically. It updates correctly if I make a button on the index page with a method that calls StateHasChanged() like this:

<button @onclick="UpdatePage">update page</button>

@code {
    private void UpdatePage()
    {
        StateHasChanged();
    }
}

I have tried to call StateHasChanged() from my child component, in the same method which sets the new value of the ClaimedBy property, but that doesn't work. StateHasChanged() has to be called from the parent component. How do I get it to update automatically once the new value has been set?

答案1

得分: 2

我通过EventCallBack实现了您的代码,它正常运行:

父组件:

@page "/parent"
@using CheckStateHasChanged.Class
@inject CaseDetails caseDetails
<h3>父组件</h3>
<p>@caseDetails.ClaimedBy声明</p>
<Child caseDetailClaimedBy="UpdatePage"></Child>

@code {
    string claimedByInParent = string.Empty;

    public async Task UpdatePage(string claimedByFromChild)
    {
        claimedByInParent = claimedByFromChild;
    }
}

子组件:

@using CheckStateHasChanged.Class
@inject CaseDetails caseDetails
<h3>子组件</h3>
<button @onclick="() => UpdatePage(caseDetails.ClaimedBy)"></button>

@code {
    [Parameter]
    public EventCallback<string> caseDetailClaimedBy { get; set; }

    public async Task UpdatePage(string claimedBy)
    {
        caseDetails.ClaimedBy = "test";
        await caseDetailClaimedBy.InvokeAsync(claimedBy);
    }
}

CaseDetails.cs:

namespace CheckStateHasChanged.Class
{
    public class CaseDetails
    {
        public string ClaimedBy { get; set; } = "admin";
    }
}

有关EventCallBack的更多信息,您可以参考组件事件在Blazor中实现事件回调的5个步骤

英文:

I implemented your code by EventCallBack and it works correctly:

Parent:

@page &quot;/parent&quot;
@using CheckStateHasChanged.Class
@inject CaseDetails caseDetails
&lt;h3&gt;Parent&lt;/h3&gt;
&lt;p&gt;Claimed by: @caseDetails.ClaimedBy&lt;/p&gt;
&lt;Child caseDetailClaimedBy=&quot;UpdatePage&quot;&gt;&lt;/Child&gt;

@code {

    string claimedByInParent = string.Empty;

    public async Task UpdatePage(string claimedByFromChild)
    {
        claimedByInParent = claimedByFromChild;
    }
}

Child:

@using CheckStateHasChanged.Class
@inject CaseDetails caseDetails
&lt;h3&gt;Child&lt;/h3&gt;
&lt;button @onclick=&quot;(()=&gt;UpdatePage(caseDetails.ClaimedBy))&quot;&gt;&lt;/button&gt;

@code {
    [Parameter]
    public EventCallback&lt;string&gt; caseDetailClaimedBy { get; set; }

    public async Task UpdatePage(string claimedBy)
    {
        caseDetails.ClaimedBy = &quot;test&quot;;
        await caseDetailClaimedBy.InvokeAsync(claimedBy);
    }
}

CaseDetails.cs

namespace CheckStateHasChanged.Class
{
    public class CaseDetails
    {
        public string ClaimedBy { get; set; } = &quot;admin&quot;;
    }
}

For more info about EventCallBack you can refer to Component events and 5-steps-to-implement-event-call-backs-in-blazor

huangapple
  • 本文由 发表于 2023年2月14日 06:33:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定