mobx-state-tree: type.models and observable array dont work together

Bug report

  • [ x] I’ve checked documentation and searched for existing issues
  • [ x] I’ve made sure my project is based on the latest MST version

Sandbox link or minimal reproduction code https://codesandbox.io/s/48mn350jv7

Or the code, if you prefer to paste it in https://mattiamanzati.github.io/mobx-state-tree-playground/ :

import React from "react"
import { types } from "mobx-state-tree"
import { observable } from 'mobx'
import { observer } from "mobx-react"
import { inspect, render } from "mobx-state-tree-playground"

const AppModel = types.model({
    count: types.optional(types.number, 10)
}).actions(self => ({
    increment(){
        self.count++
    },
	decrement() {
		self.count--
	}
}))

const ArrM = types.model( { arr: types.array( AppModel ) }) ;

const ddbb = AppModel.create()
const cache = ArrM.create( { arr: [ ddbb ] } )
const a = observable( [ cache[ 0 ] ] )
inspect(store)

const App = observer(
    props => <div>
        My awesome counter: 
        <button onClick={() => props.store[0].decrement()}>-</button> 
        {props.store[0].count} 
        <button onClick={() => props.store[0].increment()}>+</button> 
    </div>
)

render(<App store={a} />)

Describe the expected behavior I have a types.model instance (cache in the code) that consists in an array of other types.model (cache = [ ddbb ] ) instances. Getting a sublist (in this case [ cache[ 0 ] ]) and storing it in a simple observable array (I called it a) do end in throwing exception when attempting to read the value: Cannot read property ‘count’ of undefined. This is thrown in the mobx getter function.

Describe the observed behavior To read the value without any exception.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Thank you Xavier!

There is a second copy of mobx in the repository!!! after I removed by hand everything seems to work as expected.

But catching it has not been easy. The reason is that I have some local packages that I have linked using yarn link to the main project or to another of these local packages. Yarn seems to create a filesystem link in the node_modules directory to the linked package, and there is where the second copy of mobx was (the linked package contains another node_modules directory). Not easy to find as a find do not follow symlinks by default (need to add -L). No “warning message” appear in the console.

Thank you for all your help!