Search code examples
blazor-server-side

What's the difference between <MyComp><Router/></MyComp> and <MyComp/><Router/>


Is there any difference, when I have a component I am using to get some circuit properties before any other component is initialized, between:

<CircuitPropsGrabber />

<Router AppAssembly="typeof(Program).Assembly">
    <!-- lots of elements -->
</Router>

and

<CircuitPropsGrabber>

    <Router AppAssembly="typeof(Program).Assembly">
        <!-- lots of elements -->
    </Router>

</CircuitPropsGrabber>

Playing with the debugger they both seem to work the same, with <CircuitPropsGrabber> being called first. This is for render mode InteractiveServer.


Solution

  • The first happens by order. When the RenderTreeBuilder processes the Renderfragment for Routes it adds the components in order.

    The second happens by design. The RenderFragment defined as ChildContent will only be processed by the Renderer when the parent component first renders.

    If you have dependencies on the data in child components you do need to make sure the properties you grab have been grabbed before the first render event in either case. If the grab is async I would run that code in SetParametersAsync rather than OnInitializedAsync. Any await in SetParametersAsync will complete before you call the base method that then calls the lifecycle methods.