Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Is it possible to have a two way binding on component render

$
0
0

Parent component:

@page "/parent-component<ChildComponent @bind-Name="@name" /><h1>@name</h1>@code {  private string name = "Hugo";}

Child component:

<button @onclick="UpdateName">Update</button>@code {  [Parameter]  public string Name { get; set; }  [Parameter]  public EventCallback<string> NameChanged { get; set; }  private async Task UpdateName()  {    Name = $"Hi {Name}";    await NameChanged.InvokeAsync(Name);  }}

When going to the url /parent-component, the H1 says Hugo. And when I click on the button, the text updates to "Hi Hugo" - just as expected.

However, is it possible to remove the button, and have the child do it logic, in this case just to add "Hi", when the component is rendered?

I have tried using OnInitializedAsync, and OnParametersSetAsync but that don't update the value on the parent component.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>