Joey Yi Zhao
Joey Yi Zhao

Reputation: 42500

How to get a plain object from mobx object?

I defined a mobx map as below:

@observable editors = observable.map();

then I added object on the editors as below:

  editors.set(key, {
    alias: 'alias-1',
    message: 'hello',
  })

when I get the object from editor as below:

  let myEditor = editors.get(key)

the returned object myEditor has some builtin functions such as:

$mobx:ObservableObjectAdministration
get alias:function ()
set alias:function ()
get message:function ()
set message:function ()

I wander how I can get a plain javascript object from editor?

Upvotes: 11

Views: 5944

Answers (1)

Tholle
Tholle

Reputation: 112787

You can use toJS.

Example

class MyStore {
  @observable editors = observable.map({});
}

const myStore = new MyStore();

myStore.editors.set('example', {
  alias: 'alias-1',
  message: 'hello'
});

console.log(toJS(myStore.editors));

Upvotes: 8

Related Questions