BL0005-外部参数用法-为什么对此发出警告? [英] BL0005 - external parameter usage - why is a warning for that?

查看:78
本文介绍了BL0005-外部参数用法-为什么对此发出警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BL0005:Component参数不应在其组件之外设置.

BL0005:Component parameter should not be set outside of its component.

我在组件内部有一个组件.当我的外部组件状态改变时,我想更新内部组件的状态.外部组件是自动完成的实现,更改的状态是输入文本,内部组件是自动完成的项目列表,其突出显示的部分准确.

I have a component inside a component. When my outer component state changes, I want to update inner component's state. The outside component is an autocomplete implementation, the state that changes is input text, the inner component is autocomplete items list, its highlighted part to be exact.

因此,我通过其参数更改内部组件的状态.我得到这个警告.为什么?

So I change the inner component state via its parameter. And I get this warning. Why?

顺便说一句,代码的工作原理与我预期的完全一样.状态已同步,我匹配的文本在所有自动完成项中突出显示.

BTW, the code works exactly as I expected it to work. The state is synchronized, my matched text is highlighted in all autocomplete items.

我的猜测是更改参数可能导致组件重新呈现自身,对于我而言,这是完全期望和预期的行为.为什么还要在运行时更改组件的参数?

My guess is changing the parameter could cause the component to re-render itself, which is, in my case - completely desired and expected behavior. Why else would I change a parameter of a component at runtime?

我在网上搜索了警告,但是除了那个警告之外,我没有发现任何有用的东西: https://github.com/aspnet/AspNetCore/issues/12293

I searched for the warning online, but I haven't found anything useful beside that one: https://github.com/aspnet/AspNetCore/issues/12293

同样,用我在代码中修改参数的方式似乎是很不好的做法,但是,为什么呢?是我有理由以一种不太简单直接的方式做到这一点,还是有更多简单直接的方式做到这一点?

Again, it looks like modifying the parameter the way I do in my code is a very bad practice, but again, WHY? Is there a reason I have to do that in less simple and straightforward way, or there is MORE simple and straightforward way of doing this?

推荐答案

好问题!

Blazor团队非常直接地指导他们不要直接修改参数:

The Blazor team is pretty straightforward with their guideline to not modify parameters directly:

我们要使其很难或不可能设置组件参数 财产直接.在应用程序中完成时这永远是不正确的 代码.

We want to make it hard or impossible to set a component parameter property directly. This is never correct when done in application code.

来源: https://github.com/aspnet/AspNetCore/issues/8825

但是,您必须继续搜索以找出原因.根本在于渲染系统,主要在于渲染系统如何处理父组件和子组件之间的ParameterView.

However, you have to keep searching to find the reasoning for that. The root lies in the rendering system, mainly in how the rendering systems handles ParameterViews between parent and child components.

以下是史蒂夫·桑德森(Steve Sanderson)的一句话:

Here is a quote from Steve Sanderson about that:

每次重新渲染父组件(索引)时,它都会重新渲染 其子级(Counter),然后将一个值写入Content参数. [...]

Every time your parent component (Index) re-renders, it's re-rendering its child (Counter) and writing a value to the Content parameter. [...]

解决方案是不直接更改[Parameter]属性. 这样做会导致混乱,因为它们是从两个地方写的 彼此干扰的不同地方.

The solution is not to mutate your [Parameter] properties directly. Doing so leads to confusion, because they are getting written from two different places which interfere with each other.

来源: https://github.com/aspnet/AspNetCore/issues/14283#issuecomment-534651201

因此,基本上不直接修改参数的原因是为了避免存在渲染系统不同步的风险.这将导致非常细微的错误.您的代码目前可以正常工作,但不能保证在RenderTree中添加或删除组件不会导致副作用.

So basically the reason to not modify parameters diretly is to avoid running the risk that the rendering system runs out of sync. This would lead to very subtle errors. Your code is working fine at the moment but there is no guarantee that adding or removing components to your RenderTree will not result in side effects.

这篇关于BL0005-外部参数用法-为什么对此发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆