Is it OK that Composite::initComponent() called before beforeEnter() method?

Hi, devs!

I’m mastering UI based on Composite component.

But now I tried to grab some params from routing via BeforeEnterObserver and to use them in Composite::initComponent() and was confused - beforeEnter() called after initContent. :(

The reason is Vaadin uses getElement().getContent() under the hood and therefore it calls initContent():

java.lang.RuntimeException: breakpoint
	at com.citysense.elk.module.address_phone_book.profile.content.TestView.initContent(TestView.java:18)
	at com.citysense.elk.module.address_phone_book.profile.content.TestView.initContent(TestView.java:11)
	at com.vaadin.flow.component.Composite.getContent(Composite.java:129)
	at com.vaadin.flow.component.Composite.getElement(Composite.java:173)
	at com.vaadin.flow.router.EventUtil.collectBeforeEnterObserversFromChainElement(EventUtil.java:154)
	at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.sendBeforeEnterEventAndPopulateChain(AbstractNavigationStateRenderer.java:512)
	at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.createChainIfEmptyAndExecuteBeforeEnterNavigation(AbstractNavigationStateRenderer.java:482)
	at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.handle(AbstractNavigationStateRenderer.java:226)
	at com.vaadin.flow.component.internal.JavaScriptNavigationStateRenderer.handle(JavaScriptNavigationStateRenderer.java:78)
	at com.vaadin.flow.component.UI.handleNavigation(UI.java:2005)
	at com.vaadin.flow.component.UI.renderViewForRoute(UI.java:1968)
	at com.vaadin.flow.component.UI.browserNavigate(UI.java:1833)

But is it right / expected behaviour? Looks like a bug for me.

I’m using Vaadin 24.4.8.

Not sure if that is intended behavior, but if you have the feeling, that it might be a bug feel free to create a ticket in our github repo :)

1 Like

Ah, thanks :)

There is the issue: Composite::initComponent() called before beforeEnter() method · Issue #6813 · vaadin/flow-components · GitHub

1 Like

Copied @Leif’s answer from GitHub:

This is as designed. You should think of initContent as an extension of the component’s constructor. The constructor is run before beforeEnter and the same also goes for initContent.
Furthermore, there are cases where beforeEnter will be run for an “old” component instance which means that beforeEnter should anyways be implemented in a way that works regardless of the component’s lifecycle.

1 Like

So, if we need to operate with routing parameters - in view creation process or simply to reroute without creating source view - we should to use afterNavigation callback? (I already do that but maybe there is a better solution).

after navigation is the best point for things after navigation is done

1 Like