mobx icon indicating copy to clipboard operation
mobx copied to clipboard

observable object assignment, properties.hasOwnProperty

Open smikulic opened this issue 10 years ago • 5 comments

I discovered an issue when trying to assign observable object with some key value pairs to an empty observable one (they both contain "$mobx: ObservableObjectAdministration").

So, this is the object I'm trying to update: @observable filter = {};

and the function which after updating

setFilter(filter) {
  this.filter = filter;
}

disregards the assignment and shows this error: Uncaught TypeError: properties.hasOwnProperty is not a function

But, as soon as I wrap my filter in toJS() or clone():

setFilter(filter) {
  this.filter = toJS(filter);
}

it results in not having before mentioned error, which is great. But I did a bit more investigation and have seen that my filter object no longer contains "$mobx: ObservableObjectAdministration", which makes sense after toJS conversion to plain object. This also indicated that I was about to change filter object and this is why it failed (hello Immutable). So after another try I think I got the result I wanted by doing this (even though the first try already fixed the error):

setFilter(filter) {
  this.filter = Object.assign(this.filter, filter);
}

So, what could be super helpful if the error message was a bit more descriptive =)

Also, any advice in how to approach this better or should I just rely on Object.assign()? That is, any clarification about what would happen if I stopped at the first try and allowed my 'this.filter' object to become a plain object and why that didn't cause an error?

Thanks!

smikulic avatar Aug 19 '16 09:08 smikulic

Couldn't reproduce. For me everything behave as you would expect at the beginning. Could you link a fiddle or something showing error?

asterius1 avatar Aug 19 '16 10:08 asterius1

@smikulic, what mobx version are you using? Does the following code works ok in you environment? What is you babelrc config?

import { observable } from 'mobx';
import React, { Component } from 'react';
import { render } from 'react-dom';

class C extends Component {

    @observable filter = { a: 1 };

    setFilter(filter) {
        this.filter = filter;
    }

    componentDidMount() {
        this.setFilter({ a: 2 });
        console.log(this.filter.$mobx);

        this.setFilter(observable({ a: 3 }));
        console.log(this.filter.$mobx);
    }

    render() { return null; }
}

if(document.readyState === 'complete') {
    render(React.createElement(C, null), document.body);
} else {
    document.addEventListener('DOMContentLoaded', function(event) {
        render(React.createElement(C, null), document.body);
    });
}

Does it work ok without @observable decorator?

class C extends Component {
  componentDidMount() {
    extendObservable(this, { filter: {} });
  }
  // ...
}

andykog avatar Aug 21 '16 12:08 andykog

@andykog these are my current mobx packages:

"mobx": "^2.4.3",
"mobx-react": "^3.5.1”,

and babelrc:

{
  "presets": [
    "react",
    "es2015",
    "stage-1"
  ],
  "plugins": ["transform-decorators-legacy"]
}

and yea, I've added the component you suggested to my app and it seems to be working flawlessly=) So I will try to apply this to my implementation

Thanks for taking the time!

smikulic avatar Aug 22 '16 08:08 smikulic

@smikulic, you mean, its working without extendObservable? Then it's not decorator issue, might be something wrong with your code... Tell, when you will have found out.

andykog avatar Aug 22 '16 09:08 andykog

Closing for inactiviy

mweststrate avatar Sep 05 '16 19:09 mweststrate