I am trying to make a Razor component that removes itself from the page after 3 seconds, after its added to the page.
- I will click on a button
- Than the Component will be added to the current page
- After 3 seconds the Component removes itselfrom the page
<**div @ref="messageRef" style="position: absolute; margin: 0 auto; background-color: red; width: 200px; height: 80px;">**
<p>Message.....</p>
</div>
@code {
ElementReference messageRef;
private MessageComponent thisMsg;
protected override async Task OnInitializedAsync()
{
await JSRuntime.InvokeVoidAsync("MessageComponent.Remove()", messageRef);
StateHasChanged();
}
}
ifstatement would work to remove the div and paragraph from the DOM here, or is there a reason you're trying to remove the component itself? Which, btw, I don't believe is possible to do from the inside the component itself - you would have to do this at the parent level where this component's calling-markup exists.