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(UI.java:961)
	at com.vaadin.flow.component.contextmenu.ContextMenuBase.lambda$beforeOpen$504e9d67$1(ContextMenuBase.java:378)
	at com.vaadin.flow.internal.StateTree.lambda$runExecutionsBeforeClientResponse$1(StateTree.java:368)

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;

@Route("")
@PreserveOnRefresh
public class MyPage extends Div {
  public MyPage() {
	Grid<String> grid=new Grid<>();
	ContextMenu contextMenu=new ContextMenu(new Span("Click"));
	contextMenu.setOpenOnClick(true);
	contextMenu.addItem("Test");
	grid.addColumn(s->s).setHeader(contextMenu.getTarget());
	grid.setItems("A","B","C");
	add(grid);
  }
}

Which Vaadin version you use?

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

https://github.com/TatuLund/devday-demo-flow/blob/master/src/main/java/com/vaadin/devday/demo/views/GridView.java#L175

And here

https://github.com/TatuLund/devday-demo-flow/blob/master/src/main/java/com/vaadin/devday/demo/views/GridView.java#L269

Your line here is not the correct way

grid.addColumn(s->s).setHeader(contextMenu.getTarget());

Instead use

grid.getHeaderRows().get(0).getCell(grid.getColumnByKey("month")).setComponent(div);

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:

defaultHeaderRow.getCell(this).setComponent(headerComponent)

with defaultHeaderRow beeing the first header row.

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