Replies: 2 comments
-
|
any update? |
Beta Was this translation helpful? Give feedback.
-
|
In Blazor, components are created and managed by the renderer, so you generally cannot manually create an instance and control its lifecycle yourself. When you need to render components dynamically, the recommended approach is to use Example: <DynamicComponent Type="@componentType" Parameters="@parameters" />If you need to interact with the component, a common pattern is to pass a callback or service through parameters so the dynamically created component can communicate back to the parent. Directly instantiating and controlling component instances is not supported because Blazor’s rendering system manages component creation and reuse internally. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I need to create multiple components dynamically at runtime and insert them at dynamically created containers.
So at development time I don't know how many containers will be available, where are they placed and which components will be created and in which container they will be inserted. All this should happen at runtime.
With BuildRenderTree I'm able to create HTML at runtime and insert this at a specific place on my page. It is also possible to call OpenComponent on the RenderTreeBuilder but you can't hand over an instance of your component, you only have the possibility to specify the type of your component and Razor will then create an instance of your component. So you don't have the reference to this created instance and can't call methods, etc. of this component. Also everytime the BuildRenderTree method is called the same instance of the previously created component will be used by Blazor.
Is it possible to create a full component at runtime by code, have an instance of this component and place the component on your page? With BuildRenderTree, DynamicComponent and RenderFragment it seems not possible. Is there anything else or should I create an issue so this can be added in a future release?
Beta Was this translation helpful? Give feedback.
All reactions