Trying to detach an element from parent that does not have it
See original GitHub issueI 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:
- Created 3 years ago
- Comments:8 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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).
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.