Reputation: 3423
Coming from React JS, I wonder if Blazor has the concepts of State and Property. See the code below:
<div>@field</div>
<button @onclick=@(() => field = Random.Shared.Next())>Change field</button>
<div>@Prop</div>
<button @onclick=@(() => Prop = Random.Shared.Next())>Change prop</button>
@code {
private int field;
[Parameter]
public int Prop { get; set; }
}
There is absolutely no difference between field
and Prop
, except that you can set Prop
from the parent's template. I haven't been able to create a property that cannot be updated within the component, since the public setter is required.
It seems to be up to the team to decide whether it makes sense to update a property within the component or not.
Is there a convention/best practice/guideline about that?
Upvotes: 1
Views: 89
Reputation: 273494
Is there a convention/best practice/guideline about that?
Yes: do no do that.
Most 'official' is in the docs here:
Our general guidance is not to create components that directly write to their own parameters after the component is rendered for the first time.
and here:
Generally, avoid creating components that write directly to their own component parameters. ...
but that is repeated and explained in many places.
Upvotes: 2