question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trying to detach an element from parent that does not have it

See original GitHub issue

I have this code:

void addToSlot(String name, Component... components) {
    for (Component component : components) {
        var element = component.getElement();
        element.setAttribute(AttributeNames.SLOT, name);
        this.getElement().appendChild(element);
    }
}

And it works the first time, but after navigating away and back to the same route I get this exception:

java.lang.IllegalArgumentException: Trying to detach an element from parent that does not have it.
	at com.vaadin.flow.dom.impl.AbstractNodeStateProvider.removeChild(AbstractNodeStateProvider.java:123)
	at com.vaadin.flow.dom.Node.removeChild(Node.java:332)
	at com.vaadin.flow.dom.Element.removeFromParent(Element.java:577)
	at com.vaadin.flow.dom.Node.insertChild(Node.java:249)
	at com.vaadin.flow.dom.Node.appendChild(Node.java:141)
	at it.axians.vaadin.mixins.HasSlots$1.add(HasSlots.java:63)

How can I prevent this?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
cdircommented, Jan 18, 2021

I have the same problem with a Grid component. The grid is part of a tab sheet using Tabs. To not having all the tab contents in the DOM, I remove all components below grid and only add the one content I need. All components are stored in a map to do not create the whole content every time I switch the tab. Using this approach I get the exception from a component (for example a button) in a cell of the grid after switching to the corresponding tab the second time (first time the grid was added initially, second time it was re-attached).

1reaction
heruancommented, Apr 14, 2020

Some components might be Spring beans in the UI scope. A workaround I’ve found is to use removeFromTree on detach, but I’m not sure if this has side effects.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.IllegalArgumentException: Trying to detach ... - Vaadin
lang. IllegalArgumentException: Trying to detach an element from parent. I identified the problem: If I scan for meter devices ( primaryScan() ...
Read more >
Vaadin - Remove Component from its parent - Stack Overflow
removeComponent(child ); I tried to just detach the child but that removed it from the 'application' without removing the Component from the UI....
Read more >
Remove the parent Element of a Node using JavaScript
To remove the parent element of a node, select the child node and use the `parentElement` property to get access to the parent...
Read more >
Difference between remove() and detach() Methods
It does not keep the data of the removed elements. Like, all child elements, event handlers, and any type of data present inside...
Read more >
HTML DOM Element removeChild Method - W3Schools
The removed node (element). null if the child does not exist. More Examples. Remove an element from its parent ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found