Setting a component as parameter
Let's say you keep certain state of the application in AppState (e.g. IsRightMenuOpen , UserLoggedIn,..) But what I would like to see if it's possible to set the content of a component based on a parameter in the AppState. So in the MainContent there is a button that triggers the RightMenu to open and set it's Content (a BlazorComponent)
AppState.cs
public class AppState : IAppState
{
public bool IsRightMenuOpen { get; set; }
public BlazorComponent ContentOfTheRightMenu{ get; set; }
}
MainContent.cshtml
@inject IAppState appState
<div>
<button onclick="@ToggleRightMenu"> Open Right Menu With Content</button>
</div>
@functions{
void ToggleRightMenu()
{
appState.RightContent = //MyComponentName <= How can we instantiate this?
appState.ToggleRightMenu();
}
}
RightMenu.cshtml
@inject IAppState appState
<div>
@appState.ContentOfTheRightMenu
</div>
Where I fail to succeed seems to be the following line:
appState.RightContent = MyComponentName
How can we instantiate a component like this?
An alternative would be to create an enum of each components you want to render and create a switch statement in the RightMenu.cshtml but I doubt that this is the way it should work. Please note that this issue is not about tracking changes or triggering a StateHasChanged on the right component, it's simply setting the component to make it possible to render later.
You could try using RenderFragment aspnet/Blazor#723
@dlr1 Thanks, works like a charm. I'll keep this part of the code separate since it's apparently not ment to be used. Just wondering how the API to create RenderFragments could look like especially for this use case since I don't really want it to be rendered (yet) only when it hits the screen.