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.

Error "Can't move a node from one state tree to another." when re-adding a previously removed component after refreshing with @PreserveOnRefresh

See original GitHub issue

Description of the bug

If a component is added and then removed from a component that is preserved on refresh, trying to add it again after refreshing results in the error

java.lang.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

Minimal reproducible example

Code below. Steps to reproduce:

  1. Click the first button
  2. Click the second button
  3. Refresh the page
  4. Click the first button again

import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PreserveOnRefresh;
import com.vaadin.flow.router.Route;

@Route("")
@PreserveOnRefresh
public class MainView extends VerticalLayout {
    public MainView() {
        Text text1 = new Text("Text 1");
        Text text2 = new Text("Text 2");

        Div container = new Div();

        Button button1 = new Button("Set text 1", e -> {
            container.removeAll();
            container.add(text1);
        });
        Button button2 = new Button("Set text 2", e -> {
            container.removeAll();
            container.add(text2);
        });

        add(button1, button2, container);
    }
}

Expected behavior

Text 2 is replaced by Text 1.

Actual behavior

An exception is thrown

java.lang.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

Versions:

  • Vaadin 14.1.27, Flow 2.1.9
  • Java version: openjdk 11.0.4
  • OS version: Windows 10
  • Application Server (if applicable): Spring Boot embedded Tomcat

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
denis-anisimovcommented, May 14, 2020

Confirmed.

Thank you very much for precise and short steps to reproduce. 👍

0reactions
denis-anisimovcommented, Jul 28, 2020

This ticket was absolutely specific for @PreserveOnRefresh. The reason has been identified based on provided code sample and fixed. So this ticket is fixed and closed.

If you see another issue then please:

  • create another ticket.
  • provide exact steps to reproduce
  • it might be that this ticket should be made in the Dialog repository.

Please note that the exception doesn’t identify the ticket. Especially if there is no stackrace. The issue is identified by the steps to reproduce. This happens for me even without using @PreserveOnRefresh : this is a clear indicator that this is a totally different issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

“Can't move a node from one state tree to another” error in ...
Looks it is due the same reason @Sathya had mentioned above. If you add a component, twice and refresh the page, you would...
Read more >
java - "Can't move a node from one state tree to another" when ...
Whenver I click logout, I successfully get redirected to the LogIn View, but instead of logging in, it says "Assertion Error: No child...
Read more >
Preserving and Resetting State - React Docs Beta
State is isolated between components. React keeps track of which state belongs to which component based on their place in the UI tree....
Read more >
Troubleshoot—ArcGIS Online Help | Documentation
When I add an OGC WMS or WMTS layer to my map, I get an error that says the layer's coordinate system doesn't...
Read more >
Changelog | Cypress Documentation
Breaking Changes: Cypress dropped support for Node.js 12, 15 and 17. Those versions have reached end-of-life. Installing Cypress on your system ...
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