IllegalStateException in Grid with a ContextMenu in the column header

When I try to create a Grid with a ContextMenu in the column header I get an “IllegalStateException: Can’t move a node from one state tree to another. If this is intentional, first remove the node from its current state tree by calling removeFromTree” when I click on the header after the first browser tab refresh.
It happens when the ContextMenu is added:

	at com.vaadin.flow.component.UI.add(
	at com.vaadin.flow.component.contextmenu.ContextMenuBase.lambda$beforeOpen$504e9d67$1(
	at com.vaadin.flow.internal.StateTree.lambda$runExecutionsBeforeClientResponse$1(

Here is a minimal working example:

import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.contextmenu.ContextMenu;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.router.PreserveOnRefresh;
import com.vaadin.flow.router.Route;

public class MyPage extends Div {
  public MyPage() {
	Grid<String> grid=new Grid<>();
	ContextMenu contextMenu=new ContextMenu(new Span("Click"));

Which Vaadin version you use?

I have used a bit similar approach in my test app here

And here

Your line here is not the correct way


Instead use


I use Vaadin 14, currently in compatibility mode (planning to convert soon).

The code of Column::setHeader does the same thing as your expanded line:


with defaultHeaderRow beeing the first header row.

Therefore I don’t see how that could affect my problem.