Skip to content

Cannot add an object to a state tree if it is already part of the same or another state tree. #229

Closed
@shigurenimo

Description

mobx-state-tree: 0.9.0
node: 8.0.0

const {types} = require('mobx-state-tree')

const Model = types.model({
  a: types.model({b: types.string}),
  c: types.string // string
})

const Store = types.model({
  one: Model,
  index: types.array(Model)
}, {
  set (one) {
    this.one = one
  },
  push (model) {
    this.index.push(model)
  }
})

const object = {
  a: {b: 'string'}, // object
  c: new Date().toString() // string
}

const instance = Store.create({
  one: object,
  index: [object]
})

instance.set(object)

instance.push(object) // ok

↑ Even if the same object exists in the state tree, It works.

const {types} = require('mobx-state-tree')

const Model = types.model({
  a: types.model({b: types.string}),
  c: types.Date  // types.string -> types.Date
})

const Store = types.model({
  one: Model,
  index: types.array(Model)
}, {
  set (one) {
    this.one = one
  },
  push (model) {
    this.index.push(model)
  }
})

const object = {
  a: {b: 'string'},
  c: new Date() // string -> date (number)
}

const instance = Store.create({
  one: object,
  index: [object]
})

instance.set(object)

instance.push(object) // error

↑ I changed types.string to types.Date, and I received an error.

Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '/index/0/c', but it lives already at '/one/c'

What rules exist?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions