简体   繁体   English

Blazor Appstate 不更新 ChildComponent

[英]Blazor Appstate doesn't update ChildComponent

Trying to pass down variabels with an AppState class, the components updates when the call happends in the same component but not in a childcomponent when the parrent triggers the event.尝试使用 AppState class 传递变量时,当父组件触发事件时,调用发生在同一组件中而不是子组件中时,组件会更新。

Parent家长

@code {
    [Parameter]
    public int Id { get; set; }

    WikiDetailDto wikiDetail = null;

    protected async override Task OnInitializedAsync()
    {
        Console.WriteLine("going in detail INIT");
        AppState.OnCurrentWikiPageChanged += StateHasChanged;
        AppState.SetCurrentWikiPage(await wikiPageService.GetDetail(Id));
        wikiDetail = AppState.CurrentWikiPage;

        Console.WriteLine("Retrieved WikiDetail!");
    }
}

Child孩子

<MudDrawer Anchor="Anchor.End" Open="true">
    <MudDrawerHeader>Recommendations</MudDrawerHeader>
    @if(AppState.CurrentWikiPage != null)
    {
        @(RecommendedWikiPages = CalculateReccomendedWikis(AppState.CurrentWikiPage));
        if(RecommendedWikiPages != null)
        {
            foreach(var p in RecommendedWikiPages)
            {
                <MudCard>
                    <MudCardContent>
                        <MudText>@p.Name</MudText>

                    </MudCardContent>
                </MudCard>
            }
        }
    }
    else
    {
        <p>Loading...</p>
    }

When AppState.CurrentWikiPage gets updated, it doesn't check the if statement of the cild.当 AppState.CurrentWikiPage 更新时,它不会检查 cild 的 if 语句。 What is best practice here?这里的最佳做法是什么? I don't want to use cascading values.我不想使用级联值。

AppState应用状态

public WikiDetailDto CurrentWikiPage { get; set; }
        public event Action OnCurrentWikiPageChanged;
        public void SetCurrentWikiPage(WikiDetailDto wikiPage)
        {
            CurrentWikiPage = wikiPage;
            OnCurrentWikiPageChanged?.Invoke();
        }

If I'm reading your code correctly, the child component needs to register for the OnCurrentWikiPageChanged event.如果我正确阅读了您的代码,则子组件需要注册OnCurrentWikiPageChanged事件。

Your child needs to look like this:您的孩子需要看起来像这样:

@implements IDisposable
..... markup code


    protected async override Task OnInitializedAsync()
    {
        AppState.OnCurrentWikiPageChanged += StateHasChanged;
    }

    public void Dispose()
    {
        AppState.OnCurrentWikiPageChanged -= StateHasChanged;
    }

Personally I would not link StateHasChanged directly to the event handler.就我个人而言,我不会将StateHasChanged直接链接到事件处理程序。 Its bad practice because StateHasChanged has to run the the UI context thread.这是不好的做法,因为StateHasChanged必须运行 UI 上下文线程。 If the event caller is running elsewhere, you'll get an exception.如果事件调用者在其他地方运行,你会得到一个异常。 I would stick with a standard EventHandler .我会坚持使用标准的EventHandler WASM may be single threaded now, but it may not be forever. WASM 现在可能是单线程的,但可能不会永远。

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

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