简体   繁体   English

使用 StateHasChanged 从父组件刷新 blazor 服务器子组件

[英]Refresh blazor server child component from parent component by using StateHasChanged

Blazor relaod child component Blazor 重新加载子组件

One of my component is like below ( This is a child component in my case )我的一个组件如下(在我的例子中这是一个子组件)

ListMetaData component / Child Component ListMetaData 组件/子组件

  <table>
        bind the list of items from model to the table
  </table>

@code {
   
    List<MetaDataViewModel> model= new List<MetaDataViewModel>();    
    
    protected override async Task OnInitializedAsync()
    {
        await LoadMetaDataList();
    }
    public async Task  LoadMetaDataList()
    {        
        model= dbContextService.fetchFromDb();
    }    
    public void Refresh()
    {
        StateHasChanged();
    }
}

I have a parent component and it is like below我有一个父组件,如下所示

@page "/settings/metadata"
@inject IDialogService DialogService
@inject ISnackbar Snackbar
<MudGrid>
<MudItem  >
   <MudButton OnClick="@((e) => AddNewmetaDataEvent())" 
    Class="ma-1">Add new</MudButton>
</MudItem>
<MudItem xs="12" >
    <ListMetaData  @ref="_listComponent" ></ListMetaData>
</MudItem>
</MudGrid>
@code {
     private ListMetaData  _listComponent;
    async Task AddNewmetaDataEvent()
    {
        var dialog = DialogService.Show<AddMetaDataDialog>("Add new MetaData");
        var result = await dialog.Result;
        if (!result.Cancelled)
        {           
             Snackbar.Add("Please wait while the list is getting updated", Severity.Info);
             _listComponent.Refresh();             
        }
        else
        {
              Snackbar.Add("Dialog cancelled without performing any action", Severity.Normal);
        }
    }
}

From parent component on button click event a dialog is appearing and allowing user to add new item to the database.从按钮单击事件的父组件出现一个对话框,并允许用户向数据库添加新项目。

On close of dialog i am trying to reload the list of items in child component在对话框关闭时,我试图重新加载子组件中的项目列表

But the list is not getting updated for me even after using StateHasChanged().但是即使在使用 StateHasChanged() 之后,列表也没有为我更新。 I have to reload the page to see the modified list of items in the child component我必须重新加载页面才能看到子组件中修改后的项目列表

I followed couple of answers from community How to refresh a blazor sub/child component within a main/parent component?我遵循了社区如何刷新主/父组件中的 blazor 子/子组件的几个答案 but not getting what is not correct in my code但没有得到我的代码中不正确的内容

This should work (however see the notes)这应该有效(但请参阅注释)

@page "/settings/metadata"
@inject IDialogService DialogService
@inject ISnackbar Snackbar

 <MudItem  >
   <MudButton OnClick="@((e) => AddNewmetaDataEvent())" 
    Class="ma-1">Add new</MudButton>
 </MudItem>

@if (_updating)
{
<span>Updating</span>
}
else
    {
<MudGrid>

   <MudItem xs="12" >
    <ListMetaData  @ref="_listComponent" ></ListMetaData>
</MudItem>
</MudGrid>        
 }



@code {
    private ListMetaData  _listComponent;
    private bool _updating;
    async Task AddNewmetaDataEvent()
    {
        _updating = true; // forces UI dom refresh

        var dialog = DialogService.Show<AddMetaDataDialog>("Add new MetaData");
        var result = await dialog.Result;
        if (!result.Cancelled)
        {           
             Snackbar.Add("Please wait while the list is getting updated", Severity.Info);
        }
        else
        {
              Snackbar.Add("Dialog cancelled without performing any action", Severity.Normal);
        }
        _updating = false; // forces UI dom refresh
    }
}

Try to avoid updating the child components by capturing the reference.尽量避免通过捕获引用来更新子组件。 I am aware that UI libraries like SyncFusion etc force this kind of @ref / Refresh() pattern.我知道像 SyncFusion 等 UI 库强制使用这种 @ref / Refresh() 模式。 Avoid it as much as possible and instead pass on the data as parameter .尽可能避免它,而是将数据作为参数传递。 That will save you from these type of manual refreshes.这将使您免于这些类型的手动刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM