Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object dies when passed as a prop and updated in the tree #672

Closed
aripekkako opened this issue Feb 22, 2018 · 4 comments
Closed

Object dies when passed as a prop and updated in the tree #672

aripekkako opened this issue Feb 22, 2018 · 4 comments
Labels
needs reproduction/info Needs more info or reproduction in order to fix it

Comments

@aripekkako
Copy link

Short description

We're getting "This object has died and is no longer part of a state tree." error when passing a mobx-state-tree object as a prop and merging the map containing the products with a map (literal object) that has an object with the same key. We call merge() for the map where the object (product) is stored in an action (self.products.merge({ ...})).

Full error

Error: [mobx-state-tree] This object has died and is no longer part of a state tree. It cannot be used anymore. The object (of type 'ProductCollectionPropertyElement') used to live at '/productStore/products/149900/collection/column/elements/0'. It is possible to access the last snapshot of this object using 'getSnapshot', or to create a fresh copy using 'clone'. If you want to remove an object from the tree without killing it, use 'detach' instead.

Code causing the issue

The error is thrown in the line with const status = productCollection.getRowStatus( below.

  getVisibleRowElements = () => {
    const { productCollection } = this.props;
    return productCollection.row.elements.reduce(
      (elementsWithAvailability, element) => {
        const status = productCollection.getRowStatus(
          this.state.activeColumnElement.id,
          element.id
        );
        if (status.exists) {
          elementsWithAvailability.push({
            element: element,
            available: status.available
          });
        }
        return elementsWithAvailability;
      },
      []
    );
  };

What have we tried?

    getVisibleRowElements = () => {
    const { productCollection } = this.props;
    const clonedProductCollection = detach(clone(productCollection));
    return clonedProductCollection.row.elements.reduce(
      (elementsWithAvailability, element) => {
        const status = clonedProductCollection.getRowStatus(
          this.state.activeColumnElement.id,
          element.id
        );
        if (status.exists) {
          elementsWithAvailability.push({
            element: element,
            available: status.available
          });
        }
        return elementsWithAvailability;
      },
      []
    );
  };
@aripekkako
Copy link
Author

If there is any additional information I can provide to resolve this issue, I'm more than glad to provide it.

@lishine
Copy link

lishine commented Mar 6, 2018

#683 related

@mweststrate
Copy link
Member

@aripekkako if you could introduce a minimal reproduction that would be great!

I did find and PR a fix for a bug in map reconcilation today, related to either using numbers as identifier, or using the .put method. See #693. It might be related, but I cannot be entirely sure about that without a test that I can add to the test suite :)

@mweststrate mweststrate added the needs reproduction/info Needs more info or reproduction in order to fix it label Mar 9, 2018
@aripekkako
Copy link
Author

#693 shipped in v 1.4.0 fixed this issue in our app. Thank you @mweststrate for the awesome work as always.

Blessings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs reproduction/info Needs more info or reproduction in order to fix it
Projects
None yet
Development

No branches or pull requests

3 participants