Reputation: 9360
I have a parent and a child component.In the child i emit an event to the parent.However i want this Action
to be async
since i want the parent to perform an async
operation when he receives the event.How can i do this ?
Child
@functions{
[Parameter] protected Action onclose { get; set; }
[Parameter] protected Action<bool> onsubmit { get; set; } //i want this handler to be async in the parent
string campaign;
public async Task OnSubmitAsync() {
var created = await this.service.CreateCampaignAsync(parameters);
Console.WriteLine("Result of creation:" + created.ToString());
this.onsubmit?.Invoke(created);
}
Parent
<CampaignForm onclose="@(()=>OnModalClosed())" onsubmit="@(async(x)=>OnSubmit(x))"></CampaignForm>
@functions{
public async Task OnSubmit(bool value) {
//do some awaiting here
}
public void OnModalClose()=>....; //do something sync ;
}
Upvotes: 1
Views: 947
Reputation: 45586
Do this on the child component:
@functions{
// Define a property to store the Action delegate
[Parameter] protected Action<bool> onsubmit { get; set; }
// More code here...
public async Task OnSubmitAsync() {
var created = await this.service.CreateCampaignAsync(parameters);
Console.WriteLine("Result of creation:" + created.ToString());
// Call back the parent's method
onsubmit?.Invoke(created);
}
}
Do this on the parent component: Note that you should assign the identifier of the OnSubmit method to the func delegate attribute onsubmit (onsubmit="OnSubmit")
<CampaignForm onclose="OnModalClosed" onsubmit="OnSubmit"></CampaignForm>
@functions{
public async void OnSubmit(bool value) {
//do some awaiting here
}
public void OnModalClose()=>....; //do something sync ;
}
Hope this helps... Please mark my answer as accepted if it helped you out Hope this helps...
Upvotes: 1