Mobx-state-tree: Error: [mobx-state-tree] setParent is not supposed to be called on scalar nodes

Created on 18 Jul 2018  路  15Comments  路  Source: mobxjs/mobx-state-tree

[EDITED]
Change "self.items.unshift(id)" by "self.items = [id, ...self.items.slice()]" fixed issue
[/EDITED]

I have:

  • [x] I think something is not working as it should.

    • [x] I've checked documentation and searched for existing issues

Error below only happen with mobx-state-tree 3.0. I basically put a model into a array [ self.items.unshift(model) ] This model has another model that call getParent.

works perfectly with mobx 2.2

Error: [mobx-state-tree] setParent is not supposed to be called on scalar nodes
    at fail (mobx-state-tree.module.js:2286)
    at ScalarNode$$1.setParent (mobx-state-tree.module.js:885)
    at ArrayType$$1.willChange (mobx-state-tree.module.js:2979)
    at interceptChange$$1 (mobx.module.js:2688)
    at ObservableArrayAdministration.spliceWithArray (mobx.module.js:2856)
    at Proxy.unshift (mobx.module.js:3012)
    at unshift (page.js:126)
    at executeAction$$1 (mobx.module.js:613)
    at res (mobx.module.js:605)
    at runMiddleWares (mobx-state-tree.module.js:1907)

code example. I have many pages, that only keep ids, and I have a central repository to find itens with that id. I think that issue is with "get list()"

t.model('activityPage', {
  items: t.optional(t.array(t.number), [])
})

.views(self => {
  get list() { return self.items.map(id => getRoot(self).getItem('activity', id)) }
})

.actions(self => {
  unshift(id) {
    self.items.unshift(id)
  }
})
needs reproductioinfo

Most helpful comment

Released 3.2.2, please check if this solves the issue

All 15 comments

+1

+1

+1

@maxguzenski / @hiepnguyen1195 / @jeffberry the provided information is not sufficient to reproduce the issue. Please create a code sandbox demonstrating the issue

Next +1, submit a PR with test (and/or fix) or link to a sandbox. +1-ing doesn't help otherwise :)

To reproduce codesandbox

Happens when simply splicing as well. codesandbox

Edit:
Interestingly enough I pulled locally and ran the tests. I expected "paths shoud remain correct when splicing" to fail but it went through without an issue.

+1

my workaround
array.clear(); array.replace(value);

Released as 3.0.3.

I hope this solves all the cases! Otherwise, please provide additional sandboxes or PR a unit test

It seems that unshifting is still not working (prependObject):
https://codesandbox.io/s/zxx8klwq1l

The same error still happens when trying to remove an item from an array of scalars: https://codesandbox.io/s/xv2j597oq. The funny thing is that if you try to remove the last item in the array it works! Also, if your array contains a single item it also works. In all the other cases this error is thrown. Note also that the model in my sandbox doesn't even have a parent!

Released 3.2.2, please check if this solves the issue

@xaviergonz I can confirm that the issue with removing items from an array has been resolved on my end 馃帀

@Maher4Ever thanks for checking! closing

Was this page helpful?
0 / 5 - 0 ratings